简体   繁体   中英

In what cases viewWillAppear be called?

A normal answer would be, when the view will appear.

Say I have a UIViewController. Let's call that vc.

Say I want vc to control a view.

so I do vc.view = controlledView;

I expect everytime controlledView is about to appear then [vc viewWillAppear] will be called.

It doesn't.

What's wrong?

Also viewDidLoad is also not called even after I do

[vc view]

Technically, vc.view is already loaded

if you are calling viewWillAppear in any other file , with other View instance then it will never call.

viewWillAppear method calls every time when go to that view and if you leave it and then again come or come-back , it will call again.

please must use 'Super' keyword as. [super viewWillAppear] in WillAppear method.

According to docs

This method is called before the receiver's view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view.

viewWillAppear is always called when your view is about to appear, as name itself suggests.

If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.

Life Cycle of view controller goes like this:

When a viewcontroller is allocated and loaded, loadView is called then viewDidLoad is called. You can see the entire flow as in image.

Refer to this image 在此处输入图片说明

NOTE: This image is taken from this answer

The right answer is the following.

Did you add the child viewController as the child of the parent child view controller.

viewWillAppear will only be called for the parent view controller (the top screen view controller) unless the other viewController is declared as the child.

Then the parentViewController will pass on viewWillAppear events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM