I have six viewControllers. On viewController1 I have a button which takes me to the viewController6.
Now from viewController6 I want to pop back to viewController5, viewController4, viewController3, viewController2 and viewController1 respectively.
How can I do this?
Try this,
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
Insert your required view controllers in order using
[allViewControllers insertObject:viewControllerX atIndex:requiredIndex];
If you want to replace only previous viewController use,
[allViewControllers replaceObjectAtIndex:indexRequired withObject:viewControlerX];
Then set back the navigation stack,
self.navigationController.viewControllers = allViewControllers;
Then you can use following to pop back to any viewController :
[self.navigationController popViewControllerAnimated:YES];
You can use [self.navigationController popViewControllerAnimated:YES]; if you want to pop just 1 ViewController, or use [self.navigationController popToRootViewControllerAnimated:YES]; to go back to the fist ViewController
Yes this is Possible .
if you want to pop 1 ViewController from controller stack
[self.navigationController popViewControllerAnimated:YES];
go back to the fist ViewController.
[self.navigationController popToRootViewControllerAnimated:YES];
For More Information use this Link
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Use either popToViewController:animated or setViewControllers:animated
Use [self.navigationController pushViewController:]
in this order:
from ViewController1, 6, 5, 4, 3, 2.
And use [self.navigationController popToRootViewControllerAnimated:] to go from ViewController2 to 1.
Related
I have a basic doubt.I need to push viewController to another viewController .
and I am trying with code
Display1 *ac =[[Display1 alloc]init];
[[self navigationController]pushViewController:ac animated:YES];
this gives the option of navigation to previous stack.
I don't option to move on Previous Stack .for that reason I tried presentModalViewController
Display1 *ac =[[Display1 alloc]init];
[self presentModalViewController:ac animated:YES];
But this works fine this one does not give me option to but presentModalViewController Hides my UITabBarController
.is there anyway to, display UITabBarController with presentModalViewController.
or using pushViewController to not display previous stack
Darshana Is right If you dont want back option that use
self.navigationItem.hidesBackButton = YES;
before
[[self navigationController]pushViewController:ac animated:YES];
But if you want UITabBar at new UIViewController than you have to add that controller like this:
NextViewController *nextViewController=[[NextViewController alloc]initWithNibName:#"NextViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:nextViewController];
[self.navigationController presentModalViewController:navBar animated:YES];
I have take this from PresentModalViewController not showing navigation bar on next view
But first decide what you want to use PUSH OR MODAL.
this both has different purpose.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iphone - “Pushing the same view controller instance more than once is not supported” exception
I am properly pushing test2ViewController from 1 as follows,
self.controller2 = [[test2ViewController alloc] initWithNibName:#"test2ViewController" bundle:nil anUser:self.idUser];
[[self navigationController] pushViewController:self.controller2 animated:NO];
[self.controller2 release];
from 2 to 1 I pop it after initialize 1 again (necessary to initialize).
self.controller1 = [[test1ViewController alloc] initWithNibName:#"test1ViewController" bundle:nil anUser:self.idUser];
[[self navigationController] popToRootViewControllerAnimated:NO];
[self.controller1 release];
and problem appers when trying to push again 2 from 1, app crashes with an error,
Pushing the same view controller instance more than once is not supported
what am doing wrong? thank you.
Well first off you are creating another instance of test2ViewController so you will go to a different instance each time you change view.
What you should do:
if(!test2ViewController)
secondView = [[test2ViewController alloc] init...];
[self navigationController pushViewController:secondView animated:NO];
and to return, simply:
[self.navigationController popViewControllerAnimated:NO];
PoppingtoRoot causes you to pop to the very first view controller that used the pushViewController method.
Judging by the code you posted you only push one view controller (controller2) to your navigation controller.
popToRootViewControllerAnimated: will remove all view controllers from the stack except the root view controller (which seems to be controller2 in your case). So basically it does nothing.
Then you try to push the same view controller2 again and it fails, because as the error message said, that's not allowed.
You do not need to reinitialize viewController1 again; if you are pushing the viewController2 from 1, than you only have to invoke
[self.navigationController popToRootViewControllerAnimated:NO];
because viewController1 is already in the stack. What this method will do is to remove all viewControllers on the stack except the first one and move back to it.
If viewController1 is not the rootView controller you should use
[self.navigationController popViewControllerAnimated:NO];
which will pop only the last pushed viewcontroller on the stack and reveal the one beneath it.
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.
Im using Navigation Controller for my ViewControllers,I set my importantViewController as something like this to be its RootView:
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentModalViewController: navControl animated: YES];
Then, I pushView anotherView the FrontViewController like this:
[self.navigationController pushViewController:vc animated:YES];
After a button is pressed in FrontViewController another view will be pushed ViewA but it is connected with another ViewController ViewB the same way as this AGAIN:
[self.navigationController pushViewController:vc animated:YES];
(Which I think Im doing wrong when dismissing either of them with [self.navigationController popViewControllerAnimated:YES];)
This is an illustration:
My problem is, I need to navigate between View A and View B then when I dismiss either of them it will got back to FrontViewController. Like a child of a child View. Thanks.
I think this is for dismissModalViewController, but try this,
From View B write code like this
[[self parentViewController].navigationController popViewControllerAnimated:YES];
and From View A you can write,
[self.navigationController popViewControllerAnimated:YES];
Or either you can use this,
[self.navigationController popToViewController:frontViewController animated:YES];
UPDATE
for (UIViewController *tmpController in [self.navigationController viewControllers])
{
if ([tmpController isKindOfClass:[FrontViewController class]])
{
[self.navigationController popToViewController:tmpController animated:YES];
break;
}
}
This is the best solution to achieve this.
Write this code on both of your View A or B.
Hope it works now :-)
There is one way #Prasad G indicated. But problem with this solution is you need the same object of frontViewController. You can't do this with creating a new object. For going to this way declare frontViewController object in appdelgate and while pushing it from importantVC use
appdelgate.frontViewController = // initialize
// Push it
While going back from view B
[self.navigationController popToViewController:appdelegate.frontViewController animated:YES];
Another solution is
for (UIViewController *vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass:[FrontViewController class]]) {
[self.navigationController popToViewController:vc animated:YES];
break;
}
}
Using this way you can go on any of view controller from any level of navigation stack.
Using the first solution if you have 10 view Controllers and you want to go on any of one so you have to first create object of all 10 View Controller in appdelegate.
This code may have spell issues as I just typed this here
Hope this helps :)
UPDATE
->You have impVC as your root view
-> You pushed frontVC
-> From there you Pushed VC_A
-> From there you want to push VC_B
so you are done with pushing and for coming back to VC_A you can use
[self.navigationController popViewControllerAnimated];
Now you can again come on VC_B and again pop it. For going to frontVC from VC_A you can use popViewControllerAnimated and for going to frontVC from VC_B you can use the for loop i mentioned.
Please explain if you are looking anything else. If you are still facing issue please explain.
[self.navigationController popToViewController:frontViewController animated:YES];
Try like this i think it will be helpful to you.
In FrontViewController After a button is pressed:
ViewA instance
[self.navigationController pushViewController:ViewA animated:YES]
When ViewA disissmed
[self.navigationController popViewControllerAnimated:YES];
Load ViewB in ViewA
ViewB instance
[self.navigationController pushViewController:ViewB animated:YES]
On back viewB to FrontViewController
FrontViewController instance
[self.navigationController popToViewController:FrontViewController animated:YES];
On back viewB to viewA
[self.navigationController popViewControllerAnimated:YES];
I have a UIViewCOntroller, and my code is as follows.
TviewController *tviewController = [[TviewController alloc]init];
[self.navigationController pushViewController:tviewController animated:YES];
Now from TviewController i go to another viewCOntroller;
XviewController *xviewController = [[XviewController alloc]init];
[self.navigationController pushViewController:xviewController animated:YES];
in this XviewController there is a button, when i click that button i need to move BACK to the TviewController How do i do this programatically ?
note: I don't want to use pushViewController and push it further. I need to go back to the TviewController (as in clicking the back button twice)
Just
[self.navigationController popViewControllerAnimated:YES];
You should spend some time reading the guidelines about view controllers...
[self.navigationController popViewControllerAnimated:BOOL)]
[self.navigationController popToViewController:(UIViewController *) animated:(BOOL)];
[self.navigationController popToRootViewControllerAnimated:(BOOL)];
are methods to go back in hierarchy
there is 3 possible ways.
use popToRootViewControllerAnimated: -> to go back root view controller (first view controller)
use popViewControllerAnimated: -> to go backward 1. it's exact same way as back button.
use popToViewController:animated: -> to go back to UIViewController you want (as long as it's stack at back).
point 1 & 2 is pretty easy to implement and other answers lead you to there.
for point 3, here it is:
#class TviewController.h;
#interface XviewController : UIViewController
{
//this is XviewController.h
//you may use `#import` other than use `#class` but on some reason you can't, if you use`#import XviewController.h` in your TviewController class.
}
//XviewController.m
#import TviewController.h
#import XviewController.h
#implementation
-(IBAction) backTviewController
{
for ( UIViewController *viewController in self.navigationController.viewControllers )
if ( [viewController isMemberOfClass:[TviewController class]]{
[self.navigationController popToViewController:viewController animated:YES];
break;
}
}