views:

469

answers:

1

I have the UIApplicationDelegate protocol in my main AppDelegate.m class, with the applicationDidBecomeActive method defined.

I want to call a method when the application returns from the background, but the method is in another view controller. How can I check which view controller is currently showing in the applicationDidBecomeActive method and then make a call to a method within that controller?

+4  A: 

Any class in your application can become an "observer" for different notifications in the application. When you create (or load) your view controller, you'll want to register it as an observer for the UIApplicationDidBecomeActiveNotification and specify which method that you want call when that notification gets sent your your application.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod:) UIApplicationDidBecomeActiveNotification object:nil];

Don't forget to clean up after yourself! In dealloc, remember to remove yourself as the observer:

[[NSNotificationCenter defaultCenter] removeObserver:self];

You can learn more about the Notification Center here.

Reed Olsen
Excellent. Didn't think of using `NSNotificationCenter`. Thank you!
Calvin L