views:

1796

answers:

1

I'm currently having an issue with UIViewController's presentModalViewController:animated:. I use the following code to set up and show the modal view controller:

    UINavigationController *navigationController = [[UINavigationController alloc] init];
AddSerialController *serialController = [[AddSerialController alloc] initWithNibName:@"AddSerial" bundle:nil];
[navigationController pushViewController:serialController animated:NO];

[self.parentViewController presentModalViewController:navigationController animated:YES];

[serialController release];
[navigationController release];

The Application (running in iPhone Simulator) crashes as soon as dismissModalViewControllerAnimated: is called. GDB says it crashes at objc_msgSend. If I comment out the last line of code (release of the navigation controller) everything works - but I'm leaking a UINavigationController (as expected).

What the hell is going on here?

A: 

When you create a UINavigationController, you should give it a root view controller:

AddSerialController *serialController = [[AddSerialController alloc] initWithNibName:@"AddSerial" bundle:nil];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:serialController];

[serialController release];

[self.parentViewController presentModalViewController:navigationController animated:YES];

[navigationController release];

hatfinch
Thanks for your answer!Actually, the mistake was further down the road. I had some custom UITableViewCells in a table view which apparently caused the -release-problem. If the navigation controller gets released some ugly things happen to the cells - I still have to investigate what happens exactly.I'll mark this question as answered anyway - there is nothing left to fix in the presentModalViewController:animated:-part (except the root view controller thing, thanks!), so I'll maybe post another question concerning these UITableViewCell issues later.