开发者

objective-c (iphone sdk) access instance method from separate class

I know this is a pretty well posted thing to do, but I still can't work it out.

I have an instance method saveAllDataJobs in Jobs.m.

- (void) saveAl开发者_运维百科lDataJobs { ... }

I am in DetailViewController.m and I want to run the method saveAllDataJobs, which is in Jobs.m. What precisely do I need in order for this code to run.

Sorry for the repeat question, but I can't work it out.

Regards


Read about "delegation" in documents. Here is the basics:

When you create DetailViewController, you give it an ivar:

@interface DetailViewController {
    id delegate;
}

@property (assign) delegate;
@end

@implementation DetailViewController

@synthesize delegate;

@end

Then:

DetailViewController *controller = [[DetailViewController alloc] initWithNibName...]
controller.delegate = jobs; // "jobs" is of class Jobs, instantiated somewhere else

Later, when you need to call some method on jobs inside detailViewController, you do

if ([self.delegate respondsToSelector:@selector(saveAllDataJobs)]) {
    [self.delegate saveAllDataJobs];
}

There are more details around this, but this is the basic pattern.


Call the method with [someJobsInstance saveAllDataJobs] ?

Is that is not an answer to your question then you need to explain more what you are trying to accomplish. I get the feeling that this is more about application architecture than about calling methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜