I'm writing a standard table view application with a number of views in the hierarchy. When I've clicked in 3-4 views, is there a way to get back to the top view? I tried loading it, but then I lose the hierarchy.
I know this command will bring me back 1 view, which is what the 'back' button does:
[self.navigationController popViewControllerAnimated:YES];
You can use popToRootViewControllerAnimated: or popToViewController:animated: methods.
To get the viewcontroller to which you need to jump, get a list of all viewcontrollers from the navcontroller in an array and then select the viewcontroller from this array.
i.e. if your hierarchy is svc->svc2->vc1->vc2->vc3->vc4 and you want to go back to vc1 from vc4, do this
NSArray *viewControllers = [[self navigationController] viewControllers];
UIViewController *controller = [viewControllers objectAtIndex:2];
[[self navigationController] popToViewController:controller animated:YES];
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.
I am making a small app with the following view hierarchy with UINavigationController:
Login -> Options -> three different views
The problem is that I would like to navigate between the last 3 views in the following manner:
1<->2
1<->3
2<->3
i.e. to be able to switch to any view from any other view, which reminds UITabViewController functionality. So, it is not hierarchical, it is any-to-any graph. To switch between views I will use buttons in the navigation bar.
The easiest way for me is to subclass UINavigationController, add properties that correspond to my views and implement methods for switching between these views (using pushViewController and popToRootViewController). These methods will be called from the views for switching (navigating).
However the reference says that UINavigationController is not intended for subclassing.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
What do you recommend me to do?
I'll keep the UINavigationController but instead of using the usual pushViewController:, switch views like this:
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
// from here you can modify the order of controllers as much as you want
[viewControllers addObject:nextViewController];
[viewControllers removeObject:self];
[self.navigationController setViewControllers:viewControllers animated:YES];
If you don't want how the animation turns out, you can set animated:NO and either enclose setViewControllers: in an [UIView animate...] block, or add your own custom CAAnimation to the navigation controller's layer.
Use the below code to add a view controller to a navigation controller,
Navigating From first -> second
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSMutableArray *navigationarray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[navigationarray removeAllObjects];
[navigationarray addObject:secondView];
self.navigationController.viewControllers = navigationarray;
Navigating From first -> third
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
NSMutableArray *navigationarray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[navigationarray removeAllObjects];
[navigationarray addObject:thirdView];
self.navigationController.viewControllers = navigationarray;
The above code will removes all the viewControllers from the Navigation Array and places a fresh View Controller
If u want to go to a particular view controller, then use the below code...
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]
Change the index to ur view controller in the stack.
I have an app that utilizes a UINavigation controller in a storyboard to go through two UITableViews to get to the details view. I want to skip the second Table View and go straight to the Detail View. When the user taps 'back', they should see the Second Table View.
If I use
[self.navigationController pushViewController:secView animated:NO];
[self.navigationController pushViewController:thirdView animated:YES];
the app bugs out and I get
nested push animation can result in corrupted navigation bar
2012-06-11 15:02:23.695 App[3853:f803] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
I have tried
[self navigationController].viewControllers = [NSArray arrayWithObjects: dest, detView, nil];
[[self navigationController] popToViewController:detView animated:YES];
This one worked okay, but I could not get back to the First View. The back button is gone.
I would like a few pointers, please.
OK, after thinking about this, I came up with a different answer:
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers addObject:secView];
[viewControllers addObject:thirdView];
[self.navigationController setViewControllers:viewControllers animated:YES];
[viewControllers release];
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 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.