Posts Tagged ‘modalViewController’
I’ve recently been working on an app for the iPad and ran into some strange issue, when using a modalViewController. In my case, I wanted the modalViewController to show up right after the app finished launching. I also had a pretty basic viewController setup, similar to the one you get when using the “View-based Application” template.
Inside my rootViewController I wanted to call some other viewController as modalViewController right after start up. It looked pretty much like this:
- (void)viewDidLoad {
[super viewDidLoad];
//instantiate otherViewController and call it as modalViewController
OtherViewController *viewController = [[OtherViewController alloc] initWithNibName:@"OtherWindow" bundle:nil];
self.otherViewController = viewController;
[viewController release];
[self presentModalViewController:otherViewController animated:YES];
}
Unfortunately, when I tested my app nothing happened. The reason why no errors or warnings were shown was simple: The code was fine.
So, what was the problem then? After doing some investigation I found out, that the modalViewController didn’t like being called directly from viewDidLoad. Instead I had to call it with just a tiny bit of a delay.
So, I added a new method to my class:
- (void)callOtherViewController {
if (otherViewController == nil) { //only allocate, if it has not been allocated yet
//instantiate otherViewController and call it as modalViewController
OtherViewController *viewController = [[OtherViewController alloc] initWithNibName:@"OtherWindow" bundle:nil];
self.otherViewController = viewController;
[viewController release];
}
[self presentModalViewController:otherViewController animated:YES];
}
Finally, I only had to call this method from viewDidLoad with the delay mentioned above, like this:
- (void)viewDidLoad {
[self performSelector:@selector(callOtherViewController) withObject:nil afterDelay:0.1f];
}
That did the trick!
