Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guidelines for viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear

Tags:

Are there any guidelines for using these methods in the right manner? In particular, I would like to know what type of code I could use inside them.

For example, if I have to call a method that retrieves data from a WS, where do I have to call it? Where can I register/unregister a NSNotification? etc.

like image 644
Lorenzo B Avatar asked Sep 12 '11 10:09

Lorenzo B


People also ask

What is difference between viewWillAppear and viewDidAppear?

As the name suggests the viewWillAppear is called before the view is about to appear and viewDidAppear is called when view did appear.

What is the difference between viewDidLoad and viewDidAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

Which is called first viewDidLoad or viewWillAppear?

viewWillAppear(_:) Unlike viewDidLoad , viewWillAppear is called the first time the view is displayed as well as when the view is displayed again, so it can be called multiple times during the life of the view controller object.

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.


2 Answers

From UIViewController

viewWillAppear:

This method is called before the receiver’s view is about to be displayed onscreen and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with presenting the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

viewWillDisappear:

This method is called in response to a view being removed from its window or covered by another view. This method is called before the view is actually removed or covered and before any animations are configured.

Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view, or perform other relevant tasks. For example, you might use this method to revert changes to the orientation or style of the status bar that were made in the viewDidDisappear: method when the view was first presented. If you override this method, you must call super at some point in your implementation.

viewDidAppear:

You can override this method to perform additional tasks associated with presenting the view. If you override this method, you must call super at some point in your implementation.

viewDidDisappear:

You can override this method to perform additional tasks associated with dismissing or hiding the view. If you override this method, you must call super at some point in your implementation.

For further information you may check View Controller Programming Guide for iOS

like image 74
Manlio Avatar answered Sep 25 '22 01:09

Manlio


An addition to the answer: You should invoke methods like super viewWillAppear: at the beginning of your implementation, and invoke viewDidAppear: at the end of your method. Superclass should begin with the initialisation and should be last to terminate.

like image 29
Yunus Nedim Mehel Avatar answered Sep 22 '22 01:09

Yunus Nedim Mehel