How can I remove a view from navigation controller
Asked Answered
B

5

11

I want to call a new view controller and remove the current view controller from the navigation controller stack. For example. I am in view controller A and I call B.

Now I have in the stack A , B. Now I want to call C (from B). I want the stack to be A, C.

Thanks.

Bibb answered 21/12, 2011 at 14:24 Comment(0)
B
5

This is the answer.

The following code pops the current view controller.

UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
[navController popViewControllerAnimated:NO];

And this pushes the new one:

ViewControllerC *viewC = [[ViewControllerC alloc] init];
[navController pushViewController:viewC animated:TRUE];

Hope it helps!

Bibb answered 22/12, 2011 at 11:25 Comment(1)
Any idea how to do this with ARC enabled? The compiler disallows the use of retainFarquhar
L
22

In the context of ARC, here's a possible solution:

NSMutableArray* navArray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[navArray replaceObjectAtIndex:[navArray count]-1 withObject:nextViewController];
[self.navigationController setViewControllers:navArray animated:YES];

As you can tell, this code replaces the usual push code in the view you're trying to remove from stack ("B," in your question). Line 1 copies the list of view controllers from the nav-controller stack into an NSMutableArray. Line 2 replaces the last (topmost) view with the view we want to go to next ("C"). Line 3 makes the actual nav-controller's stack to be our altered array, and animates the transition to the topmost item. (Of course you can't use this code in the root viewController.)

I found an alternative way here and adapted it for ARC:

UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:someViewController animated:YES];

The first line is needed because once you've popped the current view off the stack, self.navigationController will be nil and the third line won't work. Same number of lines as the previous way, but this way works through built-in methods instead of "manually" fiddling with the stack.

Lariat answered 26/6, 2013 at 18:55 Comment(1)
Thanks your second implementation was exactly what I was looking for.Dory
B
5

This is the answer.

The following code pops the current view controller.

UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
[navController popViewControllerAnimated:NO];

And this pushes the new one:

ViewControllerC *viewC = [[ViewControllerC alloc] init];
[navController pushViewController:viewC animated:TRUE];

Hope it helps!

Bibb answered 22/12, 2011 at 11:25 Comment(1)
Any idea how to do this with ARC enabled? The compiler disallows the use of retainFarquhar
G
4

To remove the second-from-last navigation item:

NSMutableArray *navigationStack = [[NSMutableArray alloc] initWithArray: 
    self.navigationController.viewControllers];
[navigationStack removeObjectAtIndex:[navigationStack count] - 2];
self.navigationController.viewControllers = navigationStack;

For example, run this from viewDidLoad on controller C to remove controller B from the navigation stack.

Glazed answered 9/5, 2013 at 4:53 Comment(0)
C
0

I think you can do this by first popping B from navigation stack and then pushing C into it. You should be able to use [UINavigationController popViewControllerAnimated] and [UINavigationController pushViewController:animated] for this.

Cubitiere answered 21/12, 2011 at 14:59 Comment(3)
This is correct but not complete. I finally fot it working but I have to retain the controller before poping it, so I can push the other view afterwards. UINavigationController *navController = self.navigationController; [[self retain] autorelease]; [navController popViewControllerAnimated:NO];Bibb
According to Apple reference, developer.apple.com/library/ios/#documentation/uikit/reference/… UINavigationController is not intended to be subclassed. Why something so simple is so complicated?Bibb
@Bibb My bad, I have forgot about that. Disregard my previous comment and edit. I will remove them all...Cubitiere
N
-2

You can use this code.

NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers]; [navigationArray removeAllObjects]; self.navigationController.viewControllers = navigationArray;

Hope that will work for you.

Nuisance answered 19/2, 2015 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.