pina0004
pina0004

Reputation: 35

Switching from View Controller in stack to View Controller not on stack

I'm hoping you can help me out. I've designed an app that is a Tab Bar app. In the view controller for the first tab, there is a button, that when pressed, generates a modal view. I've initialized a nav controller on that modal view, because when I hit the "Save" button on my modal view (which I use to input user data), I push another table view (which shows a table of all user inputted data so far). On that stacked table view, I have a "Done" button, which when pressed, should go to another view on the tab (a progress view of user input), that is, OFF the stack.

So my question is, if I'm two controllers into the stack, how to do I pop off the stack to another view NOT on the stack? I've used the popToViewController method, but as you may have guessed, I get the "Tried to pop to a view controller that doesn't exist" message. Here's my simple code in the second view on the stack:

- (IBAction)doneButtonPressed:(id)sender 
{
    LogTableViewController *logTableViewController = [[LogTableViewController alloc]init]; 

    [self.navigationController popToViewController:logTableViewController animated:YES];

    [logTableViewController release];
}

Where LogTableViewController is not on the stack, but is rather just another target for another tab in the app. Any ideas? Thanks in advance.

Upvotes: 0

Views: 384

Answers (2)

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

I'm not entirely sure why you just can't push the new view controller to the stack, but if you need to pop to it, you can do:

    //create new VC
LogTableViewController *newVC = [[LogTableViewController alloc]init];;

//get VC stack
NSMutableArray * newControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];

//choose where to insert the new vc
NSUInteger insert_index = [newControllers count] - 1;

//insert into the stack
[newControllers insertObject:newVC atIndex:insert_index];

//replace stacks
[self.navigationController setViewControllers: newControllers];

//pop to your new controller
[self.navigationController popViewControllerAnimated:YES];

hope this helps.

Upvotes: 1

gschandler
gschandler

Reputation: 3208

You were close. Just do:

- (IBAction)doneButtonPressed:(id)sender 
{
    LogTableViewController *logTableViewController = [[LogTableViewController alloc]init]; 

    [self.navigationController setViewControllers:[NSArray arrayWithObject:logTableViewController] animated:YES];

    [logTableViewController release];
}

Upvotes: 0

Related Questions