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.
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With