UINavigationController - implementing back to beginning button [duplicate] - iphone

This question already has an answer here:
popping to root view in navigation controller
(1 answer)
Closed 8 years ago.
I have a UINavigationController which has several screens that implement a logical set of stepped tasks ...
Is there a way that on the last screen I can have logic on a done button that would take me to the start without having to step back through the screens ?
I am really looking for code examples of the logic I would need to put on the done button?
Many thanks

You have to pop directly to rootview controller instead of just popping.
use:
[self.navigationController popToRootViewControllerAnimated:YES];

use this
[self.navigationController popToRootViewControllerAnimated:YES];

You have two options to achieve this thing on DONE button ;
1) If your rootview is the view from where you want to restart than you can use as follows -
[self.navigationController popToRootViewControllerAnimated:YES];
2) Else you can re-initialize the navigation Controller & push to the start view.
Hope this will help.

popToRootViewControllerAnimated -> It will pops all the view controllers on the stack except the root view controller and updates the display.
[yourNavigationController popToRootViewControllerAnimated:YES];
If you want to use popToViewController then
id rootView=[[self.navigationController viewControllers] objectAtIndex:0];
[self.navigationController popToViewController:rootView animated:YES];

Related

Navigating through UIVIewControllers [duplicate]

This question already has answers here:
A home button in iOS 5, xcode 4.2, Story board
(2 answers)
Closed 8 years ago.
From the FirstView to the SecondView i will be navigating through pushViewController.
In the SecondView there's a button, when i click that i will be calling another view which is called ThirdView, and i will be navigating through presentModalViewController animated:YES.
I need a code, to go from the ThirdViewController to FirstViewController, How can i do that ?
I have not tried any code, because i am clueless as what to do
You can use delegate to move back to the first view controller... On cancel button in the THIRD, delegate some method to the SECOND, where you:
1) [self dismissModalViewControllerAnimated:YES];
and
2) [[self navigationController] popViewControllerAnimated:NO];
FIRST would be active as a result
I flagged as a duplicate but you are getting a lot of misleading answers here, so I'll re-post my answer from the other question:
popToRootViewControllerAnimated: on UINavigationController will clear out the stack and return you to the root controller. There is no need to individually dismiss each controller in the stack.
In your second view controller add method like
-(void)goToFirstViewController{
[self.navigationController popViewControllerAnimated:NO];
}
In third View controller
UIViewController *secondVC = [self parentViewController];
// If you use ios 5 do [[self presentingViewController] popViewController];
[secondVC goToFirstViewController];
[self dismissModalViewController];

I can came back to first navigation control page with a button?

-(IBAction) btnReturn:(id) sender{
firstView * firstview =[[firstView alloc]initWithNibName:#"firstView" bundle:nil];
[self.view pushViewController:firstview animated:NO];
}
with the previsly code I see the first view but the navigation control increment. I wold came bak as was the navigation starting point. Any help?
pushViewController:animated: will add to the navigation stack; you want popViewControllerAnimated: to go back one view in the stack.
If you want to return to the very first (root) view controller, you want popToRootViewControllerAnimated:.
See: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
In your above code you are pushing a controller on self.view (firstView, I am considering it as controller as you are creating it using initWithNibName: method, but you should you proper naming conventions to avoid confusions.) But view do not have any such method pushViewController:. Instead you sould use if you really have self (the controller in which you are using this IBAction) in navigation stack.
[self.navigationController pushViewController:firstview animated:NO];
To pop controller from navigation stack, follow what #gregheo suggested.
Try using popViewControllerAnimated instead:
- (IBAction)btnReturn:(id)sender
{
[self.navigationController popViewControllerAnimated:NO]; // Or Yes if you would like to have an animation.
}

Memory leak on popToViewController

Hi
I am using following method to go back to one of the previous view. This is working. But I got two issues with this.
This line gives a memory leak when I use Instrument.
After popup to particular view, when I press left navigation button (back button) just only this button will disappear and view will remain.
Can anyone please let me know how to overcome these issues?
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
Thank you
try [self.navigationController popViewControllerAnimated:YES]; if you just with to remove current view from the view hierarchy,
If you, as you describe, just wants to pop just the active viewController, you can use
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
documentation here.
If this still give problems, there is something wrong with your viewController hierarchy.
Hi agree with the above answers
try these methods carefully
[self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController popToViewController:(UIViewController*)
animated:(BOOL)];
Working with iOS5.
[self.navigationController popViewControllerAnimated:(BOOL)];

pushViewController only works with animated:YES

I have found a strange behaviour when trying to push a viewcontroller onto the stack. The following works fine:
[self.navigationController pushViewController:myViewController animated:YES];
but if I change it to animated:NO it no longer works, doesn't seem to push at all. I was performing this in a viewWillAppear but I have also tried it in viewDidAppear but with no luck.
Any ideas what could be causing this?
Thanks
The problem is most probably not the call itself, but the placement of the call. Try putting the same action on a UIButton and it should 100% work. I've noticed that putting view controller manipulation routines like presentModal... and pushViewController... don't sometimes work in the viewWill* viewDid* methods. Or try making the calls from those functions with a performSelector:withObject:afterDelay after a short delay and see if that works.
Edit: there are a couple of ways of doing what you want to do. You can directly modify the navigation stack of a navigation controller, so when you are in view N+1, you can replace view N on the stack (by building a new navigation stack array and setting it in to the navigation controller), then pop, and you'll get the effect of "popping back to a different view controller". You can also issue multiple pops and pushes from the view controller you want to leave, but you have to be careful:
// pop back 2 controllers on the stack to the setup screen
//
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
//
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
//
[[self retain] autorelease];
// Pop back 2 controllers to the setup screen
//
[navController popViewControllerAnimated:NO];
[navController popViewControllerAnimated:YES];

to load view from second view to back view

i have application in which i have display question on allcard view and all answer are displayed on backside of view.i done this by addsubview:ansview in allcardmethod.
now i have onr button that will on ansview function of that button is that to go to allcard to dispaly new question but i can not achived this....
please help
if you can use navigation Controller than use
[self.navigationcontroller pushViewController:viewController animated:YES];
where viewController is object of your controller class
and also you can use
[self.navigationController popToViewController:YES];
if u can add view using addSubView method than
use removeFromSuperView method
If i understand it correctly,you want to remove your second view from super view.try this code
[ansview removeFromSuperview];
also try this
[self.navigationController popViewControllerAnimated:YES];