Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a specific view controller from uinavigationcontroller stack?

I have a uinavigationcontroller. After logged in i want to remove viewcontrollers like RegisterViewController,LoginViewController etc from UInavigationcontroller stack..

I mean i have to remove a particular view controller from stack ? How its possible. ?

I checked this post

http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller’s-root-view-controller/

So we can take it into an array like

NSArray *allviewcontrollers= [(UINavigationController *)navigationController viewControllers]; 

But how to do further process.. This question is hunting me for long time..Please answer me ..Thanks in advance

like image 310
S.P. Avatar asked Jan 20 '10 05:01

S.P.


People also ask

How do I remove a view controller from navigation stack?

Use this code and enjoy: NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self. navigationController. viewControllers]; // [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack.


2 Answers

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: navigationController.viewControllers]; [allViewControllers removeObjectIdenticalTo: removedViewController]; navigationController.viewControllers = allViewControllers; 
like image 150
Costique Avatar answered Sep 26 '22 01:09

Costique


Here is my solution. You can set the tag or a fixed property to your viewcontrollers, then you could traverse the uinavigationcontroller stack to search the target viewcontroller(vcToRemove) and remove it. This could be safer. The code:

NSInteger tag = vcToRemove.wvTag; NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: vcToRemove.navigationController.viewControllers];         int i = 1;         for (i = 1; i <=[allViewControllers count]; i++) {             UIViewController *vc = [allViewControllers objectAtIndex:i];             if ([vc isKindOfClass:[MyWebViewController class]]) {                 if (((MyWebViewController *)vc).wvTag == tag) {                     [allViewControllers removeObjectAtIndex:i];                     break;                 }             }         }          vcToRemove.navigationController.viewControllers = allViewControllers; 
like image 37
Jie Liu Avatar answered Sep 24 '22 01:09

Jie Liu