How to tell the active view controller when applicationDidBecomeActive is called?

I feel I am missing a trick here...

I just want to call viewDidLoad or viewDidAppear on the current active view controller when applicationDidBecomeActive gets called, so I can reset some animations or whatever, when the app is started up again from the background. Some of my views don't care, but others really need to know.

I am using Storyboards and my app delegate file has the standard functions - but all with EMPTY bodies. For example, didFinishLaunchingWithOptions just returns YES and does nothing else. Storyboard automagically does everything I guess.

So how can I talk to the current view controller from my rather blank, information free, app delegate?


I would recommend using notifications.

In your app delegate's applicationdidBecomeActive method put in this code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];

In your current active view controller's init method subscribe to the notification.

[[NSNotificationCenter defaultCenter] addObserver:self 

Implement the "updateStuff" method in your controller and you should be able to do whatever you want when the app becomes active.

Instead of sending a notification from your app delegate, the OS sends a notification automatically that you can observe:

[[NSNotificationCenter defaultCenter] addObserver:self

and of course make sure to stop observing sometime before or inside your dealloc method, by calling:

[[NSNotificationCenter defaultCenter] removeObserver:self 

Swift version:

You can add this row in your viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(viewDidBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)

func viewDidBecomeActive(){

Rather than trying to keep track of which ViewController is current, you could send a NSNotification from your AppDelegate and subscribe to it in your ViewController.. That way the view controller keeps track of whether or not it needs to call viewDidAppear.

your AppDelegate will have a window property, that window will have a rootViewController property. You can find your viewController here.

If you are using a TabBarController, the rootviewcontroller will be the tabbarcontroller, and you can call the tabbarcontroller's selectedViewController to get the current viewController.

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
if ([rootViewController isKindOfClass:[UITabBarController Class]])
    rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
else if ([rootViewController isKindOfClass:[UINavigationController Class]])
    rootViewController = ((UINavigationController *)rootViewController).topViewController;

[rootViewController viewDidAppear];

If you have a more complex view hierarchy with navigation controllers, or modal views, you can call on presentedViewController, or topViewController.

Need Your Help

prevent screen capture in Android apps

android screenshot

I was wondering if there is a way to detect through some "onXXXXX" call-back method or received broadcast if some other process is about to take a screen-shot of my app's display. For example if ...

How to use library in maven repo for clojure project?

maven clojure leiningen

I use Leiningen to manage my CLJ project. When I want to wrap a Java library, I found that I have to introduce it to my project firstly.