Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 311
David John Avatar asked Apr 27 '12 23:04

David John


People also ask

When applicationWillEnterForeground is called?

applicationWillEnterForeground is called: when app is relaunched(comes from background to foreground) This method is not invoked when app starts for the first time i.e when applicationDidFinishLaunch is called but only when comes from background applicationDidBecomeActive.

Which one view controller life cycle methods call every time in IOS?

viewWillAppear: This method is called every time before the view is visible to and before any animation is configured. In this method view has bound but orientation is not set yet.

How do I switch between view controllers?

From the Library, add a button to the first View Controller and name the button, for example: Show Second View . Select the button, press and hold the Control key on the keyboard and drag from the button to the second View Controller.


1 Answers

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

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

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

[[NSNotificationCenter defaultCenter] removeObserver:self                                        name:UIApplicationDidBecomeActiveNotification                                        object:nil]; 
like image 140
Ben Baron Avatar answered Oct 04 '22 20:10

Ben Baron