UINavigation to Show the third view - iphone

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];

Related

popToViewController can't switch between views

I have this function in an IBAction button:
I start off with a table with a button, when that button is pressed this takes place:
TabBarController *v = [[TabBarController alloc]
initWithNibName:#"TabBarController" bundle:nil];
[self.navigationController pushViewController: v animated:YES];
[v release];
On the second view there is another button which when pressed does this:
NSArray *array = [self.navigationController viewControllers];
[self.navigationController
popToViewController:[array objectAtIndex:1] animated:YES];
This causes the app to crash, and at index 0 it does nothing. Is this because the second page is not a UITable like the first?
I want to be able to move from UITable view to UIView back and forth, should I use addSubview instead? I need the first view to have a navbar but the second shouldn't.
I think the problem is that [array objectAtIndex:1] is your current ViewController, so popping to the current view controller might be the reason of your crash (did not try it though)
So why don't you do:
[self.navigationController popViewControllerAnimated:YES];
simply add the following line in button code and it will work fine
[self.navigationController popViewControllerAnimated:YES];

Return to the first index of UINavigationController?

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;

How can I go back to the first view controller?

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.

UINavigationController popViewController is popping everything!

So I'm trying to pop a view controller off the stack when an error occurs, but it seems like it's popping too much off in one go. The navigation bar up the top loses its title and buttons, but the old table view data remains visible. I have no idea what's going on...
The basic set up is:
Tab View template
Navigation controller
View controller (Loaded from the xib)
View controller (Pushed, what I want to pop)
Here's the code:
NSLog(#"%#", [[self navigationController] viewControllers]);
[[self navigationController] popViewControllerAnimated:NO];
NSLog(#"%#", [[self navigationController] viewControllers]);
The resulting NSLog's show:
2009-09-22 19:57:14.115 App[34707:550b] (
<MyViewController: 0xd38a70>,
<MyViewController: 0xd36b50>
)
2009-09-22 19:57:14.115 App[34707:550b] (null)
Anyone have experience with this?
I am seeing some odd UINavigationController stack behavior with just using
[self.navigationController popViewControllerAnimated:YES];
inside of a delegate called out of a tableView:didSelectRowAtIndexPath: call.
The views don't work right, and a UINavigationController provided "back" operation doesn't correctly pop the view of this delegate, going back another level.
I found that if I used
[self.navigationController popToViewController:self animated:YES];
instead, that suddenly everything worked fine. This is in an application with ARC turned on.
So, I can only guess that there is some reference housekeeping that doesn't happen correctly unless you tell it to pop back to a specific view controller when you will pop a view controller that will immediately become "unreferenced" by that pop.
I fixed it. The code that was popping the view was getting called in viewDidLoad. This meant it was getting popped before the view had actually animated in completely.
I moved that code to viewDidAppear and now it works as advertised.
[[self navigationController] popViewControllerAnimated:NO];
//here you pop **self** from navigation controller. And now
[self navigationController] == nil;
// And
[nil viewControllers] == nil
Try to do this:
UINavigationController *nc = [self navigationController];
NSLog(#"%#", [nc viewControllers]);
[nc popViewControllerAnimated:NO];
NSLog(#"%#", [nc viewControllers]);
What is wrong with?
[self.navigationController popViewControllerAnimated:NO];
Also, you may want to check how you push the ViewController onto the stack. Something doesn't sound right.

Backing up 2-4 previous views

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];