buckminsterfuller
buckminsterfuller

Reputation: 1

How to call a method that's declared in a ViewController from the AppDelegate

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.

Upvotes: 0

Views: 3000

Answers (3)

Henrik Erlandsson
Henrik Erlandsson

Reputation: 3831

Use this simple NSNotificationCenter example. Works a charm!

Upvotes: 0

Jaime
Jaime

Reputation: 6814

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];

Upvotes: 3

Aaron Saunders
Aaron Saunders

Reputation: 33345

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?

Upvotes: 0

Related Questions