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

吃可爱长大的小学妹 提交于 2019-12-25 03:26:09

问题


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) saveAllDataJobs { ... }

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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2155256/objective-c-iphone-sdk-access-instance-method-from-separate-class

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