views:

328

answers:

3

I am using one navigation controller in my application. I am having one main view (with main view controller) and few options views. Options views are viewed by navigation controller when a button clicked on main view's toolbar.

Everything works as expected for first time. When I came back to main view from navigation controller and tries again to go to option view (i.e. navigation controller) my application crashes.

Following is my code,

//Jump to navigation controller from main view controller

optionsViewController *optionsView = [[optionsViewController alloc] initWithNibName:@"optionsView" bundle:nil];
navControllerSettings = [[UINavigationController alloc] initWithRootViewController:(UIViewController *) optionsView];
[self presentModalViewController:self.navControllerSettings animated:YES];

//Code to go back to main view from navigation controller

[self.navigationController dismissModalViewControllerAnimated:YES];

What is correct mechanism to handle navigation controller? Do I need to release/dealloc the navigation controller or options view?

Sample code will help better.

A: 

Your navigation controller only gets set up once, then you push and pop other views from its stack.

UINavigationController *navController = [[UINavigationController alloc] init];
UIViewController *yourMainViewController = [[yourMainViewControllerClass alloc] init];

// when you are ready to go to your options view
optionsViewController *optionsView = [[optionsViewController alloc] initWithNibName:@"optionsView" bundle:nil];

[navController pushViewController:optionsViewController animated:YES];

// the back button and the pop from the stack when it is hit is handled auto-magically for you
mmc
A: 

Thanks for the reply.

But my application architecture is, I am having one main view on which I display few things and having toolbar. Toolbar contains one button, after clicked on that this navigation controller gets created and displayed on the screen.

using back button I came on the main view. But then after I could not go on the navigation again as it is crashing while executing following line

[self presentModalViewController:self.navControllerSettings animated:YES];

What might be the problem here?

Vishal N

A: 

I had a similar problem to this with it crashing in presentModalViewController: on second attempt. The problem was caused by calling [self becomeFirstResponder] in viewDidAppear: inside the first UIViewController that I presented, but I failed to call [self resignFirstResponder] in viewWillDisappear:.

davidcann