presentModalViewController:animated: crash after dismiss - iphone

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?

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];

Related

Popping to first view controller of the project

I have a UINavigationController. I load a presentmodalviewcontroller over it. And I push 2 more view controllers over the presentmodalviewcontroller. If I need to move to my first view controller, what should be done?
Edit: I am also loading some UIView over the UIViewController on top of my stack. I have successfully removed that.
I have tried
[self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
but still its not working
This is how I am adding each view controller
First viewcontroller
FirstViewController *firstViewController =
[[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UINavigationController *navcontrol =[[UINavigationController alloc]initWithRootViewController:firstViewController];
[self presentModalViewController:navcontrol animated:YES];
[navcontrol release];
Second viewcontroller
SecondViewController *secondViewController = [[SecondViewController alloc]init] ;
[self.navigationController pushViewController: secondViewController animated:YES];
[secondViewController release];
Third viewcontroller
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
[self.navigationController pushViewController: thirdViewController animated:YES];
[thirdViewController release];
Ok. I got it.
myAppDelegate *appDelegate = (myAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.navigationController dismissModalViewControllerAnimated:YES];

Why is my popToRootViewControllerAnimated not getting called?

I have even tried implementing a delay right before the call to popToRootViewControllerAnimated however that does no good either. It just does not get called.
-(IBAction) btnSignOut{
[[self tabBarController]setSelectedIndex:0];
[[self navigationController]popToRootViewControllerAnimated:NO];//DOES NOT GET CALLED
Overview *overviewController = [[Overview alloc] initWithNibName:#"Overview" bundle:nil];
//Lets place OverView in navController
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:overviewController];
// [[self navigationController] popToViewController:ComposeViewController animated:YES];
//Now lets display it
[self.tabBarController presentModalViewController:navController animated:YES];
[navController release];
[overviewController release];
}
It seems like the [self navigationController] does not contain any view to pop

Remove modalViewController from view?

I have a view which pulls in another view using this code:
secondView = [[SecondView alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondView];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
The cancel button on this 2nd view has this:
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
But it doesn't appear to do anything. The view is still there and I can still interact with it. How do i remove it?
try:
[self.navigationController dismissModalViewControllerAnimated:YES]

call navigationcontroller with modalview?

I have a problem, I have made this:
Impostazioni *impostazioni=[[Impostazioni alloc] initWithNibName:#"Impostazioni" bundle:nil];
[self.navigationController presentModalViewController:aiuto animated:YES];
But I want to preserve the navigation bar in this modality:
[self.navigationController pushViewController:aiuto animated:YES];
[self.navigationController.navigationBar.backItem setTitle:#"iCalory"];
And I want use presentModalView how can I this? thanks
To add navigation controller in your code,
Impostazioni *impostazioni=[[Impostazioni alloc] initWithNibName:#"Impostazioni" bundle:nil];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController: impostazioni] autorelease];
[self.navigationController presentModalViewController:navigationController animated:YES];
Just add your view controller to a new navigation controller and present the navigation controller modally:
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController: aiuto] autorelease];
[self presentModalViewController navigationController];

presentModalViewController inside UIViewController class

View *view1 = [[View alloc] init];
[self presentModalViewController:view1 animated:YES];
The code above works when connected to a UIButton. It does not work upon launch of an application by putting it in the viewDidLoad: method. I would like to run this upon launch.
Look very closely at the method you're calling: presentModalViewController: presents a controller, not a view.
The correct pattern is something like this:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self presentModalViewController:myViewController animated:YES];
[myViewController release];
Since iOS 6 you should use following method, because of deprections:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self presentViewController:myViewController animated:YES completion:nil];