popToViewController can't switch between views - iphone

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

Related

Navigation Controller Back Functionality

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

Navigating between 2 ViewControllers

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

UINavigation to Show the third view

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

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.

how to navigate from subview to another view

I want to navigate from subview to another view.i have written the code but it is navigating from this view.please help me in solving this problem.
NSLog(#"selected tag:%d",[sender tag]);
UserViewController *uViewController=[[UserViewController alloc] initWithNibName:#"UserViewController" bundle:nil];
uViewController.usersArray=self.usersArray;
uViewController.accessingIndex=[sender tag];
self.userViewController=uViewController;
[self.navigationController pushViewController:self.userViewController animated:YES];
[uViewController release];
You can use this method to show a view
[self presentModalViewController:uViewController animated:YES]
To close it use:
[self dismissModalViewControllerAnimated:yes]