oberbaum
oberbaum

Reputation: 2448

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) 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

Upvotes: 0

Views: 1439

Answers (2)

Jaanus
Jaanus

Reputation: 17856

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.

Upvotes: 1

Stefan Arentz
Stefan Arentz

Reputation: 34935

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.

Upvotes: 0

Related Questions