I have an 3 view A B C both three have an navigation bar. my need is when i click on C view Back button i want to open A view but Problem is this when i back to A. At A's Navigation Bar Adding C back button.
I tried this one
UIBarButtonItem * back=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(move)];
self.navigationItem.leftBarButtonItem=back;
-(void)move
{
FirstViewController * fvc=[[FirstViewController alloc]init];
[[self navigationController]pushViewController:fvc animated:NO];
}
please help me out this thanks in advance.
View Controller are pushed into stack at index 0,1,2 like this that means FirstViewController At 0 index,SecondViewController At 1 index as it is.
so you may try this
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
0 index For your First View Controller.
Try this one really Helpful to you.
UIBarButtonItem * back=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(move)];
self.navigationItem.leftBarButtonItem=back;
if you want to push on another view controller then use this
-(void)move
{
FirstViewController *fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.navigationController pushViewController:fvc animated:YES];
}
if you want to back to the previously screen then use this code
-(void)move
{
[self.navigationController popViewControllerAnimated:YES];
}
tyr this one
-(void)Back
{
NSArray *array1 = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array1 objectAtIndex:1] animated:YES];
// objectAtIndex put your as requirement like 0,1,2 etc..
}
I think you should use
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:VCIndex] animated:YES]
Try
-(void)move
{
[navigationController popToRootViewControllerAnimated:YES];
}
I have an application in which i am having a table view.when pressing a button on the cell i have to go to another view controller(ie:testviewcontroller) showing what i selected in the previous one.then when pressing one button i need to go to another view controller showing the remaing values.if he select one there then i need to repeat the above process.The problem is from here also i am carrying some values to that testviewcontroller.if i pop back to my view controller how can i carry the new values.currently i am doing like this.
TestViewController *test =[[ TestViewController alloc]initWithNibName:#"TestViewController" bundle:nil];
test.itemselected=head;
test.itemid=productid;
//NSLog(#"%#",del.navigationController);
[del.navigationController pushViewController:test animated:YES];
but i know
NSArray *array = [del.navigationController viewControllers];
[array objectAtIndex:3]is my desired view controller.
can anybody know how can i avoid this pushing of same view controller again?
for (UIViewController*vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass: [TestViewController class]]){
vc.itemselected= head ;
[[self navigationController] popToViewController:vc animated:YES];
}
}
*EDIT*
This should be
for (TestViewController*vc in [self.navigationController viewControllers])
instead of
for (UIViewController*vc in [self.navigationController viewControllers])
You can traverse loop on view controller stack.
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
NSMutableArray *ViewControllerArray=[[NSMutableArray alloc]init];
for (long i=[self.navigationController.viewControllers count]-1;i>0; i--) {
[ViewControllerArray addObject:[self.navigationController.viewControllers objectAtIndex:i]];
NSLog(#"%#",ViewControllerArray);
NSLog(#"%#",self.navigationController.viewControllers);
}
for (UIViewController *controller in self.navigationController.viewControllers) {
//Do not forget to import AnOldViewController.h
if ([controller isKindOfClass:[YourViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
}
[super viewWillDisappear:animated];
}
For your condition it is beter to pop back and reload the table with new contents..no need to push to a new instance
Use a seperate array for datasource of table
Remove the selected item from datasource array
Then push to next view when poped back reload contents
I use UINavigation controller and have a question. Is there way to switch from third view to first view?
Method where I create new object FirstView *controller; [self.navigationcontroller pushViewController:controller animated:YES]; is not suitable for me (losing transfered data from parent to first view).
Try this . it will work.
You can get all the View controllers in an array and pop to a specific view controller
. This code will pop to your FirstView Controller.
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];
Assuming that you know your view controller to return to and it is stored in returnToViewController:
[self.navigationController popToViewController:retunToViewContoller animated:YES];
You can use:
[[self navigationController] popViewControllerAnimated:YES];
to go back one view controller; or:
[self.navigationController popToRootViewControllerAnimated:YES];
to go back to the very first view controller in the hierarchy.
I'm doing an application which uses a UINavigationController and I'm switching to other UIViewControllers as follows:
if(self.myViewController == nil){
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
self.myViewController = aViewController;
[aViewController release];
}
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myNavController pushViewController:myViewController animated:YES];
I imagine this is creating a pile of UIViewControllers into the UINavigationController, maybe an array of indexs? I would like to know how to turn back without having to be back one by one.
For example, I'm sailing through a few screens and with a button I would like to return at the first index of navigation. I would also like know how to modify indexes, view, erase and anything pertaining to this issue.
Sorry if I have not explained well.
You've asked two questions.
The first is, how do I get back to my first view controller. As #Thomas Clayson and #ender have answered, you want the popToRootViewControllerAnimated: method of your navigationcontroller object for that.
The second is how to move to a particular index in the view controller stack. The answer to that is, you can set the array of viewControllers explicitly. So you can pull out the current listing of view controllers, modify it, and set it back into the navigationController stack. It'll reset the stack and animate you moving to the top item in the stack.
Thusly:
NSMutableArray *controllers = self.navigationController.viewControllers;
[controllers removeObjectAtIndex:[controllers count] - 1]; //or whatever
[self.navigationController setViewControllers:controllers animated:YES];
NSArray *viewControllers = [[self navigationController] viewControllers];
for (int i = 0; i < [viewContrlls count]; i++){
id obj = [viewControllers objectAtIndex:i];
if ([obj isKindOfClass:[yourViewControllername class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
Using this you can come back to any specified viewController.
[self.navigationController popToRootViewControllerAnimated:YES];
Will take you back to the very first view controller (root view controller).
Hope this helps
Use this
NSArray *viewContrlls=[[NSArray alloc] initWithArray:[[self navigationController] viewControllers]];
id obj=[viewContrlls objectAtIndex:1];
[[self navigationController] popToViewController:obj animated:YES];
[viewContrlls release];
You should use popToRootViewControllerAnimated: From UINavigationController class reference:
Pops all the view controllers on the stack except the root view
controller and updates the display.
You can return to the first view with
[self.navigationController popToRootViewControllerAnimated:YES];
That being said, you can also remove a particular view controller, or navigate to a specific index in your view controller if you look at the example.
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// You can now manipulate this array with the methods used for NSMutableArray to find out / perform actions on the navigation stack
[allViewControllers removeObjectIdenticalTo: removedViewController];
// You can remove a specific view controller with this.
navigationController.viewControllers = allViewControllers;
I have an application where I need to remove one view from the stack of a UINavigationController and replace it with another. The situation is that the first view creates an editable item and then replaces itself with an editor for the item. When I do the obvious solution within the first view:
MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];
[self retain];
[self.navigationController popViewControllerAnimated: NO];
[self.navigationController pushViewController: mevc animated: YES];
[self release];
I get very strange behavior. Usually the editor view will appear, but if I try to use the back button on the nav bar I get extra screens, some blank, and some just screwed up. The title becomes random too. It is like the nav stack is completely hosed.
What would be a better approach to this problem?
Thanks,
Matt
I've discovered you don't need to manually mess with the viewControllers property at all. Basically there are 2 tricky things about this.
self.navigationController will return nil if self is not currently on the navigation controller's stack. So save it to a local variable before you lose access to it.
You must retain (and properly release) self or the object who owns the method you are in will be deallocated, causing strangeness.
Once you do that prep, then just pop and push as normal. This code will instantly replace the top controller with another.
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
// Pop this controller and replace with another
[navController popViewControllerAnimated:NO];
[navController pushViewController:someViewController animated:NO];
In that last line if you change the animated to YES, then the new screen will actually animate in and the controller you just popped will animate out. Looks pretty nice!
The following approach seems nicer to me, and also works well with ARC:
UIViewController *newVC = [[UIViewController alloc] init];
// Replace the current view controller
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeLastObject];
[viewControllers addObject:newVC];
[[self navigationController] setViewControllers:viewControllers animated:YES];
From experience, you're going to have to fiddle with the UINavigationController's viewControllers property directly. Something like this should work:
MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];
[[self retain] autorelease];
NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
[controllers removeLastObject];
self.navigationController.viewControllers = controllers;
[self.navigationController pushViewController:mevc animated: YES];
Note: I changed the retain/release to a retain/autorelease as that's just generally more robust - if an exception occurs between the retain/release you'll leak self, but autorelease takes care of that.
After much effort (and tweaking the code from Kevin), I finally figured out how to do this in the view controller that is being popped from the stack. The problem that I was having was that self.navigationController was returning nil after I removed the last object from the controllers array. I think it was due to this line in the documentation for
UIViewController on the instance method navigationController
"Only returns a navigation controller if the view controller is in its stack."
I think that once the current view controller is removed from the stack, its navigationController method will return nil.
Here is the adjusted code that works:
UINavigationController *navController = self.navigationController;
MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];
NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
[controllers removeLastObject];
navController.viewControllers = controllers;
[navController pushViewController:mevc animated: YES];
Thanks, this was exactly what I needed. I also put this in an animation to get the page curl:
MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];
UINavigationController *navController = self.navigationController;
[[self retain] autorelease];
[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration: 0.7];
[UIView setAnimationTransition:<#UIViewAnimationTransitionCurlDown#> forView:navController.view cache:NO];
[navController popViewControllerAnimated:NO];
[navController pushViewController:mevc animated:NO];
[UIView commitAnimations];
0.6 duration is fast, good for 3GS and newer, 0.8 is still a bit too fast for 3G..
Johan
If you want to show any other view controller by popToRootViewController then you need to do following:
UIViewController *newVC = [[WelcomeScreenVC alloc] initWithNibName:#"WelcomeScreenVC" bundle:[NSBundle mainBundle]];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeAllObjects];
[viewControllers addObject:newVC];
[[self navigationController] setViewControllers:viewControllers animated:NO];
Now, all your previous stack will be removed and new stack will be created with your required rootViewController.
I had to do a similar thing recently and based my solution on Michaels answer. In my case I had to remove two View Controllers from the Navigation Stack and then add a new View Controller on. Calling [controllers removeLastObject]; twice, worked fine in my case.
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
searchViewController = [[SearchViewController alloc] init];
NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
[controllers removeLastObject];
// In my case I want to go up two, then push one..
[controllers removeLastObject];
navController.viewControllers = controllers;
NSLog(#"controllers: %#",controllers);
controllers = nil;
[navController pushViewController:searchViewController animated: NO];
This UINavigationController instance method might work...
Pops view controllers until the specified view controller is the top view controller and then updates the display.
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
Here is another approach that doesn't require directly messing with the viewControllers array. Check if the controller has been pop'd yet, if so push it.
TasksViewController *taskViewController = [[TasksViewController alloc] initWithNibName:nil bundle:nil];
if ([navigationController.viewControllers indexOfObject:taskViewController] == NSNotFound)
{
[navigationController pushViewController:taskViewController animated:animated];
}
else
{
[navigationController popToViewController:taskViewController animated:animated];
}
NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
for(int i=0;i<controllers.count;i++){
[controllers removeLastObject];
}
self.navigationController.viewControllers = controllers;
My favorite way to do it is with a category on UINavigationController. The following should work:
UINavigationController+Helpers.h
#import
#interface UINavigationController (Helpers)
- (UIViewController*) replaceTopViewControllerWithViewController: (UIViewController*) controller;
#end
UINavigationController+Helpers.m
#import "UINavigationController+Helpers.h"
#implementation UINavigationController (Helpers)
- (UIViewController*) replaceTopViewControllerWithViewController: (UIViewController*) controller {
UIViewController* topController = self.viewControllers.lastObject;
[[topController retain] autorelease];
UIViewController* poppedViewController = [self popViewControllerAnimated:NO];
[self pushViewController:controller animated:NO];
return poppedViewController;
}
#end
Then from your view controller, you can replace the top view with a new by like this:
[self.navigationController replaceTopViewControllerWithViewController: newController];
You can check with navigation view controllers array which you give you all view controllers that you have added in navigation stack. By using that array you can back navigate to specific view controller.
For monotouch / xamarin IOS:
inside UISplitViewController class;
UINavigationController mainNav = this._navController;
//List<UIViewController> controllers = mainNav.ViewControllers.ToList();
mainNav.ViewControllers = new UIViewController[] { };
mainNav.PushViewController(detail, true);//to have the animation
Alternatively,
You can use category to avoid self.navigationController to be nil after popViewControllerAnimated
just pop and push, it's easy to understand, don't need to access viewControllers....
// UINavigationController+Helper.h
#interface UINavigationController (Helper)
- (UIViewController*) popThenPushViewController:(UIViewController *)viewController animated:(BOOL)animated;
#end
// UINavigationController+Helper.m
#implementation UINavigationController (Helper)
- (UIViewController*) popThenPushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIViewController *v =[self popViewControllerAnimated:NO];
[self pushViewController:viewController animated:animated];
return v;
}
#end
In your ViewController
// #import "UINavigationController+Helper.h"
// invoke in your code
UIViewController *v= [[MyNewViewController alloc] init];
[self.navigationController popThenPushViewController:v animated:YES];
RELEASE_SAFELY(v);
Not exactly the answer but might be of help in some scenarios (mine for example):
If you need to pop viewcontroller C and go to B (out of stack) instead of A (the one bellow C), it's possible to push B before C, and have all 3 on the stack. By keeping the B push invisible, and by choosing whether to pop only C or C and B altogether, you can achieve the same effect.
initial problem
A -> C (I want to pop C and show B, out of stack)
possible solution
A -> B (pushed invisible) -> C (when I pop C, I choose to show B or also pop it)
I use this solution to keep the animation.
[self.navigationController pushViewController:controller animated:YES];
NSMutableArray *newControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[newControllers removeObject:newControllers[newControllers.count - 2]];
[self.navigationController setViewControllers:newControllers];