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.
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];
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?
Use this simple NSNotificationCenter
example. Works a charm!
精彩评论