Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewWillAppear being called twice in iOS5

I'm running all my apps to make sure it's not just one app, and in every app I have, when I run on the iOS5 simulator or device, the viewWillAppear method gets called twice on every view. I have a simple NSLog(@"1");, and this appears twice in my console every time. Is this just me, or is something going on? (It only gets called once in iOS4)

This is the code calling the view that calls viewWillAppear twice:

     CloseDoorViewController *closeVC;

     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            closeVC = [[ CloseDoorViewController alloc] initWithNibName:@"CloseDoorViewIpad" bundle:nil];
        } else {
            closeVC = [[ CloseDoorViewController alloc] initWithNibName:@"CloseDoorViewController" bundle:nil];
        }

        [self.view addSubview:closeVC.view];
        [self presentModalViewController:closeVC animated:NO];
like image 644
Snowman Avatar asked Oct 18 '11 16:10

Snowman


People also ask

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.

Can viewDidAppear be called multiple times?

viewDidAppear callback can be called multiple times in a single presentation.

Is viewWillAppear called before viewDidLoad?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is the difference between viewDidLoad and viewWillAppear?

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. Let me show you a simple experiment to help you to understand how it works.


2 Answers

It's the -addSubview: method.

When adding or removing view controller's view, someone must call 'View Event' methods such as -viewWillAppear:, etc. of the view controller.

Actually, it wasn't a recommended way to -addSubview:/-removeFromSuperView view controller's view by yourself before iOS 5, because it doesn't call 'View Event' methods (you can/should call it by yourself). Instead, it was recommended to use 'indirect' way to do that, such as -presentModalViewController: you use (it does call 'View Event' methods on your behalf).

On iOS 5, Apple has changed the behavior of -addSubview:/-removeFromSuperView methods to allow direct view management of view controller. So now, when you use those methods on viewController's view, 'View Event' methods will be called automatically.

So it was called twice.

See video "Implementing UIViewController Containment" on here also.

like image 83
ppm Avatar answered Sep 28 '22 00:09

ppm


Because you are displaying the view twice.

First time by adding the view as a subview of the current view:

[self.view addSubview:closeVC.view];

Second time by pushing the view's controller on top of current view's controller:

[self presentModalViewController:closeVC animated:NO];

I'm not sure why in iOS4 the viewWillAppear was only called once, because iOS5 is correct to call it twice, given that you are displaying the view twice as explained above.

Just remove one of the lines and it would be fine (I'd recommend removing the addSubview and retain the presentModalViewController one).

like image 36
Lukman Avatar answered Sep 27 '22 22:09

Lukman