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.
Related
I want to make a back button from a modal view to the Main view.
The View Controller is embedded in the navigation controller. The menu button take me to the Second View Controller. There I have a back button who works fine using this:
[self.navigationController popViewControllerAnimated:YES];
I want to go back to the Main View Controller from the Page Two VC.
I have tried:
- (IBAction)goToRootView:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
and:
- (IBAction)goToRootView:(id)sender {
[self dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
The first just goes back to the Second VC, the last sends and lldb error.
How can I go from Mantras Page Two VC to Main VC?
Thank you for your help!
You can tray this...
CHS_View_Controller *oldView = [self.storyboard instantiateViewControllerWithIdentifier:#"CHS_View"];
UINavigationController *yourNavigationController = [[UINavigationController alloc] initWithRootViewController:oldView];
yourNavigationController.modalTransitionStyle= UIModalTransitionStyleCrossDissolve;
[self presentViewController:yourNavigationController animated:YES completion:nil];
for that you must:
1) import you dest controller
#import "CHS_View_Controller.h" // your controller name
and
2)
set an Identifier for your CHS_Controller, "CHS_View" in the bellow example (into the Storyboard Editor and in the Attributes Inspector)
In the first snippet, instead of
[self.navigationController popViewControllerAnimated:YES];
try
[self.navigationController popToRootViewControllerAnimated:YES];
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.
I have a table in my pop up view. But when clicking on a table row, it doesn't navigate to another view controller. Is there any direct method to navigate from popup view to another view controller ?
thanks in advance.
Code::
ProductListView1 *p = [[ProductListView1 alloc]initWithNibName:#"ProductListView1" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:p animated:YES];
Did you try [self presentViewController:p animated:YES completion:nil];
If you don't have a UINavigationController than you can't push a UIViewController.
Also note, that if you push a UIViewController in a UIPopoverController, it will just enlarge the UIPopover. If you want a whole new view to be displayed, than you must use presentViewController: and you could put a UIToolbar to a UINavigationController onto that modal view.
ContentViewController of popover, should be a navigation controller.
First create navigationController with rootViewController as tableViewController(first controller to be displayed in popover)
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
and then present navigationController in popover
[popOver setContentViewController:navigationController animated:YES];
And then you can push another controller in tableViewController's didSelectRowAtIndexPath: delegate method
ProductListView1 *p = [[ProductListView1 alloc]initWithNibName:#"ProductListView1" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:p animated:YES];
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 want to go back to the first view controller. So, I used [self.navigationController popToRootViewControllerAnimated:NO] from the third view. But, it goes back to just the second view. Do I have to use popToViewController: animated: instead?
I pushed the third view like this:
[self.view addSubview:secondController.view]; // from the first view
[self.navigationController pushViewController:thirdController animated:YES]; // from the second view
Remove the second view, before using [self.navigationController popToRootViewControllerAnimated:NO]:
UIView *v = [self.navigationController.viewControllers objectAtIndex:1];
[v removeFromSuperview];
EDIT:
I am doing like this and works ok (I use this on my third view on the stack):
NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[allControllers removeObjectAtIndex:1];
[self.navigationController setViewControllers:allControllers animated:NO];
[allControllers release];
[self.navigationController popViewControllerAnimated:YES];
It looks like your navigationController was initiated when pushing the thirdController. Your secondController was not 'pushed' by the navigationController, it was added as a subview, which is quite different. So, when you push the thirdController from the secondController, it thinks your rootController is the secondController.
You have two options here:
Change the way you are presenting the secondController to actually
have the navigationController push it, or
Remove the secondController from view before the thirdController is presented.
You may be able to popToViewController, as you mentioned...I'm not positive if that will work, but it's possible.