0

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
pina0004
  • 35
  • 5

2 Answers2

1

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.

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
0

You were close. Just do:

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

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

    [logTableViewController release];
}
gschandler
  • 3,208
  • 16
  • 16
  • This will remove all other view controllers from the stack, possibly releasing them if they are not retained otherwise! Instead of replacing all view controllers with new table containing just one view - add the one to the table, then do navigation. – Peter Sarnowski Dec 08 '11 at 05:26
  • Yes, that is true. It is up to him whether he wants to keep them around or not. What he was trying to do, had it worked as he intended, would have popped them off the stack and (presumably) pushed his new one on. Of course it doesn't work that way, but `-setViewControllers:animated:` does. – gschandler Dec 08 '11 at 05:35
  • Thanks! That did it. I appreciate your help. Nice to know I was close, but I would have never come up with that on my own. Only problem is, that once I get to that new view the tab bar is not at the bottom. Any ideas as to how I can get to the original tab bar with the LogTableView showing? – pina0004 Dec 08 '11 at 06:10
  • Are you not using a `UITabBarController` as your root view controller? That would probably save you a lot of headaches. Otherwise you will need to maintain a reference to your `UITabBar` with your application delegate (since it is your central object) to be accessible by any object or view controller. Alternatively, subclass the `UINavigationController' and managed it though that. Also, be sure to mark your question as answered . – gschandler Dec 08 '11 at 06:24
  • Yes, my tab bar is the root controller for the app. Here's the code: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. logViewController.managedObjectContext = self.managedObjectContext; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES; } with IBOutlet UITabBarController in the .h file. Yes, I think I understand that as well - I do need that tab bar to be accesible by other views and stacks. Thanks. – pina0004 Dec 08 '11 at 06:43