PrEV
Thoughts from a NeXTStep Guy on Cocoa Development

Presenting Modal Views Bug

Apr 25, 2009 by Bill Dudney

While continuing on the retooling of the iPhone book I ran into an irritating bug that has a very simple work around.

When you present a modal view controller via the presentModalViewController:animated: method the viewWillAppear: method gets called on the modal view controller before the view is actually loaded. This is a pain in the neck for me because I set the values for my GUI bits in this method. It was very irritating to see all my outlets being nil when they clearly should have been set to the connections I made in IB.

I found the bug by breaking in a much later method (one of the actions my modal VC implemented) and of course everything was set up as it should be (otherwise the action would not have been invoked). That led me to implement viewDidLoad and that is where I found that viewDidLoad was being called after viewWillAppear:

Fortunately this bug has a really simple work around. Before you present your view controller make it load its view by calling the view method. Here is some sample code.

- (IBAction)modal {
  [self.second view];
  [self presentModalViewController:self.second animated:YES];
}

Hope this helps someone avoid the hour or so it took me to track this down. I would expect this to be fixed in a upcoming release so this is not a long term need, but for your 2.2 apps it will hopefully help.

Permalink     7 Comments - Add Yours