0
votes

I don't get the delegate logic of UIViewController.

It has delegate method of

– viewWillAppear: – viewDidAppear: – viewWillDisappear: – viewDidDisappear:

So, every time, if a view of a viewcontroller appear or disappear, the according above methods inside the viewcontroller will be called?

I have two viewcontrollers. viewcontroller2's view (view2) is a sub view of viewcontroller1's view (view1).

if I set view2's alpha to 0, then this view2 will disappear. but – viewWillDisappear: and – viewDidDisappear: of viewcontroller2 are never called.

So what's the point please?

How should I use these methods to control the appear and disappear of a view?

Thanks

2

2 Answers

2
votes

Those delegates are delegates of the viewController, not the subViews added to the viewController's view. You need to set the alpha of the individual subViews manually.

– viewWillAppear: – viewDidAppear: – viewWillDisappear: – viewDidDisappear:

...are called when your viewController is instantiated, or removed from the screen intentionally, or in response to a low memory condition.

0
votes

Jack,

The UIViewController lifecycle methods, viewDidLoad, viewWillAppear, etc. are called at various points throughout the view controller's lifecycle.

"Appearing", according to these methods, has nothing to do with alpha of the viewController's view. A view has appeared if a part of it's frame is the top-most level in the view hierarchy. So, if you covered a view up completely and then removed the covering view, you will get a viewWillAppear. Whereas, if you just adjust the alpha, the view is still technically "visible".

viewDidLoad is called after all loading has been completed (included from you nib files). This is the place to set up your IB Controls. When using viewWillAppear, you cannot assume things have been loaded. In most cases, all interface configuration is best done in viewDidLoad.

Also, it is important to note that these methods are delegated. That is, you will never call the explicitly.

I hope this clarifies both the lifecycle methods and the definition of "appear" therein.

Good luck!