views:

48

answers:

2

Hello.

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  A: 

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.

Jordan
sorry, didn't say clearly. edited, and now it should be clear. what I refered to is UIViewController and I understand the four methods are for UIViewController
Jack
Again, the alpha property has nothing to do with the delegate methods listed above. Instantiation, memory, dealloc, or intentionally removing viewController (e.g. pushViewController) will call those delegates. Take a look a the ViewControllers Programming Guide.
Jordan
A: 

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!

DexterW
Thanks dexter. What if I set view.hidden = YES? will the view's controller's didDisappear be called?
Jack
I have seen this post, it is quite different from what you said. http://stackoverflow.com/questions/1514098/whats-the-proper-way-to-add-a-view-controller-to-the-view-heirarchy
Jack
Yes, if you manually add a sub-view controller, you do have to call the lifecycle methods. I meant you never do things like [self viewDidAppear] Forgive me for not being clear on that.
DexterW
So you mean I should do [viewController2 viewDidAppear] in ViewController1?
Jack