Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewWillAppear not called after press home button then launch again

Tags:

ios

I make a test app and runs it on device. At frist launch everything works fine. Then I press home button to exit, then press the app icon. It seems that viewWillAppear is not called this time. In my understanding, viewWillAppear is called every time the view shows up on screen no matter it's triggered by dismiss view controller or by pressing home button then relaunched.

like image 757
Philip007 Avatar asked Aug 30 '12 13:08

Philip007


People also ask

Why viewWillAppear is not called?

The Simple Answer. The technical reason for when viewWillAppear gets called is simple. Notifies the view controller that its view is about to be added to a view hierarchy. It can't be any view hierarchy — it has to be the one with a UIWindow at the root (not necessarily the visible window).

How many times is viewWillAppear called?

If both these methods are called, what's the difference between the two? viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears.

Does viewDidLoad get called before viewWillAppear?

Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called. This gives you a chance to do any last-minute view setup, kick off a network request (in another class, of course), or refresh the screen.

How many times is viewDidLoad called?

It is called every time your view controller's view is loaded, not just the first time. @RobNapier you mean if the viewController is set to nil and later loaded again it's called again.


1 Answers

Add this in your viewDidLoad() method

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

and execute the code that needs to be ran when application becomes active again in the onResume method.

like image 148
Michael Avatar answered Sep 20 '22 20:09

Michael