0

Novice question. I'm building an app and I want to call a method that I've declared in a ViewController - from the AppDelegate (on applicationDidBecomeActive).

So basically, In TestAppDelegate.m I have...

  - (void)applicationDidBecomeActive:(UIApplication
 *)application {
     // I want to call a method called "dothisthing" that I've defined in
 FirstViewController.m
     // This does not work:  [FirstViewController dothisthing] }

In FirstViewController.m I have...

- (void) dothisthing {
   NSLog(@"dothisthing");
}

This is my first iPhone app, so any help would be appreciated.

3 Answers3

3

The method is an instance method so you need to create the instance first and then call the method on the instance... or declare the method as static (+) instead of (-) before the void

FirstViewController* controller = [FirstViewController alloc];

[controller dothisthing]; 

[controller release];
Jaime
  • 6,736
  • 1
  • 26
  • 42
  • Thanks for the clarification. I'm not sure exactly where I should create the instance first though. In AppDelegate.m? Complete Obj-C novice here. – buckminsterfuller Jul 21 '10 at 16:34
  • Well, that depends on the lifetime of your controller... if this is the main/only controller in your app, then the appDelegate should 'own' the controller (declare it in the @interface), instantiate it in the applicationDidFinishLoading and release it in the dealloc) – Jaime Jul 21 '10 at 16:44
  • Thanks! I have things working, but one more question... I get a warning: FirstViewController may not respond to '-dothisthing' Here's the code: FirstViewController *controller = [FirstViewController alloc]; [controller dothisthing]; // this throws the warning – buckminsterfuller Jul 21 '10 at 19:38
  • Whith out looking at the code it may be difficult to say, but you may be missing the declaration of the dothisthing method in the interface (.h) file, or the signature may not match the way you're calling it (ie parameters in the declaration and not calling it with params) – Jaime Jul 21 '10 at 20:30
0

you could also create a notification in the FirstViewController, to have that method called when your application becomes active.

There is a similar question where I have posted the code snippets for that option

How to refresh UITableView after app comes becomes active again?

Community
  • 1
  • 1
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
0

Use this simple NSNotificationCenter example. Works a charm!

Community
  • 1
  • 1
Henrik Erlandsson
  • 3,797
  • 5
  • 43
  • 63