choise
choise

Reputation: 25244

How to call a method of a specific class from another class (created in this specific class)?

I created a TTModelViewController. in the createModel Method i created a TTURLRequestModel. after Loading content in the TTURLRequestModel i want to call a method in my TTModelViewController.

TTModelViewController

- (void) createModel {
     requestModel = [[singlePostModel alloc] initWithId:@"54"];
}

- (void)didLoadModel:(BOOL)firstTime {
      NSLog(@"loaded");
}

TTURLRequestModel (singlePostModel)

- (void)requestDidFinishLoad:(TTURLRequest*)request {
      //doing something
     [super requestDidFinishLoad:request];
}

first i thought "didLoadModel" gets called after requestDidFinishLoad was called, but its before.

So, how can i call a method in my TTModelViewController after request is finished loading? is there a method that already does that and i only have to overwrite this? or something else?

thanks

// if knowbody knows how to do this with three20, anybody can tell me how to do this in general?

edit


the solution of the first post works fine now, but i still got a warning that the function i call to my object doesnt exist:

@interface TTModelViewController {
}

- (void)modelFinishedLoading;

@end

and now i call that method in my models class

[controller modelFinishedLoading];

at this point xcode throws a warning "no -modelFinishedLoading method found".

why? i implemented it in the interface and also in the implementation part of TTModelViewController.

does that have to do with this @class at my singlePostModel?

@class singlePostViewController;
@interface singlePostModel : TTURLRequestModel

Upvotes: 1

Views: 819

Answers (1)

Ed Marty
Ed Marty

Reputation: 39700

The way I would do it would be to subclass TTURLRequestModel, which I suspect you've already done based on the name of your class (singlePostModel). In that subclass, add a member variable that you can use to point back to your TTModelViewController. Something like:

Class Definition:

class TTModelViewController;
@interface singlePostModel : TTURLRequestModel {
    TTModelViewController *controller;
} 

- (id) initWithId:(String *)id forController:(TTModelViewController *)controller;

@end

Implementation:

@implementation singlePostModel {
- (id) initWithId:(String *)id forController:(TTModelViewController *)mvc {
  if (self = [super initWithId:id]) {
    controller = mvc;
  }
}

- (void)requestDidFinishLoad:(TTURLRequest*)request {
  [controller callMyMethodHere];
  [super requestDidFinishLoad:request];
}

@end

When you initialize it then, you would use:

- (void) createModel {
  requestModel = [[singlePostModel alloc] initWithId:@"54" forController:self];
}

Unless you have another way to get to your TTModelViewController, like through the app delegate singleton, in which case, that could work just as well, as long as you don't mind the coupling.

For example, your app delegate has a reference to the main View Controller for the application because it is set up from the main NIB. That is easily accessible using

((MyAppDelegate *)[[UIApplication sharedApplication] delegate]).myMainViewController

Upvotes: 2

Related Questions