0

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

Septiadi Agus
  • 1,775
  • 3
  • 17
  • 26

4 Answers4

4

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.

Mohit Tomar
  • 5,173
  • 2
  • 33
  • 40
2

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.

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • 2
    note that just because the view is completely offscreen or covered, `viewWillAppear` won't be called when the view "appears" unlike some of the other answers suggest, only when it's actually added to the view hierrarchy – Turch Jul 17 '13 at 05:20
2

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 enter image description here

NOTE: This image is taken from this answer

Community
  • 1
  • 1
DivineDesert
  • 6,924
  • 1
  • 29
  • 61
  • No it's not. It is normally. However, if you change the controller view to something else then it's not called. The controller do not observe the view. – Septiadi Agus Jul 18 '13 at 13:24
0

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.

user4951
  • 32,206
  • 53
  • 172
  • 282