How to call a method of another Class?

五迷三道 提交于 2019-11-29 15:56:47

Update: You should probably subclass UITableViewCell rather than UITableView. Then in your table view controller's cellForRowAtIndexPath: method, return an instance of this subclass rather than an instance of UITableViewCell.

You will also need to pass a DetailedViewController pointer on to the cell, so that you can invoke its moveToNextItem method in the touchesBegan, etc. methods.

Adapt this example to your needs:

MyTableViewCell.h

@class DetailedViewController;
@interface MyTableViewCell : UITableViewCell {
    DetailedViewController *dvc;
}
@property (nonatomic, retain) DetailedViewController *dvc;
@end

MyTableViewCell.m

#import "MyTableViewCell.h"
#import "DetailedViewController.h"
@implementation MyTableViewCell
    @synthesize dvc;
    - (void)someMethod { // This would be your touchesBegan, etc. methods
        [dvc moveToNextItem];
    }
    - (void)dealloc {
        [dvc release]; // We retained dvc so we have to release it when we're done with it
        [super dealloc];
    }
@end

DetailedViewController.h

@interface DetailedViewController : UITableViewController {
    // iVars here
}
// Methods and properties here
- (void)moveToNextItem;
@end

DetailedViewController.m

#import "DetailedViewController.h"
#import "MyTableViewCell.h"
@implementation DetailedViewController
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
        if(cell == nil) {
            cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyTableViewCell"] autorelease];
            cell.dvc = self; // This gives the cell a reference to the detailed view controller
        }
        return cell;
    }
    - (void)moveToNextItem {
        // ...
    }
@end

There are probably far better ways to achieve what you want, but this is the best I can do without more information.

Declare the method in DetailedViewController.h, and @import that file in SpecificTable.h.

if SpecificTable is really a subclass of DetailedViewController you can call

[self moveToNextItem];

as already mentioned.

but i think you mean a subview or not? so SpecificTable.view is a subview ob DetailedViewController.view

you have several options then. for example using NSNotificationCenter.

or what is probably also a good way for you is to setup an instance variable of DetailedViewController in your SpecificTable and assign it when you init your SpecificTable.

as an example:

// the parent view .m
testTVC *tableview = [[testTVC alloc] initsomething];
tableview.parentVC = self;
[self.view addSubView:tableview.view];
[tableview release];

now in your testTVC

// the .h
@interface testTVC : UITableViewController {
    testVC *parentVC;
}

@property(nonatomic,retain) testVC *parentVC;

@end


// the .m
[parentVC moveToNextItem];

you also have to synthesize and release your parentVC.

Is [NSNotificationCenter defaultCenter] something you are looking for?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!