why I can't pop the last view in the navigation stack? - ios

I want to pop the last view in my app, so I'm calling this function:
[self.navigationController popViewControllerAnimated:YES];
and self is the last view controller, I expect that the app will exit, but that does't happen why ?

For going to the rootView u need to call as below,
[self.navigationController popToRootViewControllerAnimated:YES]; // Returns the root/last vie controller.
and the one u used is below,
[self.navigationController popViewControllerAnimated:YES]; // Returns the popped controller.

If you wish to exit your app, just do that:
[[NSThread mainThread] exit]
Anyway, your way:
[self.navigationController popViewControllerAnimated:YES];
It's common in android, not in iOS.

Related

How to return to last view without pop

On didSelectRowAtIndexPath I want to return to the previous viewcontroller. I'm using this code, at the moment:
[self.navigationController popViewControllerAnimated:YES];
But, I don't want to pop from navigationcontroller. Because, when the user "clicks" on back button, I want to return to this view.
So, I want to back to previous controller without popping it from navigationcontroller. It is possible?
Thanks!
Try using [self presentViewController:vc animated:YES completion:nil]; instead
It seems that you want to push the previous view controller ,you can use the UINavigtionController viewControllers attribute.
So you code may like this
[self presentViewController:[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers.count-2]] animated:YES completion:nil];

pushViewController in viewWillAppear failed after popToRootViewController

there have 3 view controllers, A,B,C, all designed in storyboard.
in all pushViewController: controllers were got from [self.navigationController instantiateViewControllerWithIdentifier:#"A/B/C_Controller"];
In A_ViewController ( root controller ):
-(void)viewWillAppear
{
[self.navigationController pushViewController:B_Controller animated:YES];
}
In B_ViewController:
-(IBAction)someButtonClick
{
[self.navigationController pushViewController:C_Controller animated:YES];
}
In C_ViewController:
-(IBAction)someButtonClick
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Problem:
A_viewController -> B_viewController -> C_viewController is fine.
but after popToRootViewController to A From C, A can't push to B anymore.
and other pushViewController: all can't works, popViewControllerAnimated: also cause crash.
Note.
have tried
// in A view
dispatch_async(dispatch_get_main_queue(),^{
[self.navigationController pushViewController:B_viewController animated:YES];
})
in this post, but it doesn't works for me.
does anyone know what happens in view controller stacks?
have struggling in this issue all day, any help will be appreciated!
When you When you popToViewController from C, the viewWillAppear method will in A be called just before the animation. Now, at this point, since the transition is in progress, you might get some kind of warning in the debugger like:
"Warning: Attempting to push .... while a transition is in progress"
You need to call the navigation controller methods in such a way that they do not overlap each other.
Calling pushViewController in the viewDidAppear method of A would be better in this context.

Dismiss two TableViewControllers

I have a View that has a button in it, something like this Button---(segue)---> TableViewControllerNO1-----(segue)-->TableViewControllerNO2. In the TableViewControllerNO2 I delete all the rows by pressing a button and I want after I delete the rows to return to the first View, the one before the TableViewControllerNO1, how can I dismiss the two TableViewControllers from TableViewControllerNO2?
You have a cool method to do this: popToViewController
[self.navigationController popToViewController:viewController animated:YES];
So the important is inject a reference to that viewController.
Otherwise you can also do:
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController popViewControllerAnimated:YES];
but the right way is the first.
You should use - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated (assuming you are using a navigation controller). This would take you all the way back to the root view controller in your navigation stack, and use the standard animation.

IOS: dismiss two viewController

I have three viewController
First, Second and Third
from Second to open Third I use
Third *third = [[Third alloc]initWithNibName:#"Third" bundle:nil];
[self presentModalViewController:third animated:YES];
[third release];
Now I want return from third to first; then I set in viewDidAppear in second this code:
[self dismissModalViewControllerAnimated:NO];
but for 1 second I see Second and I don't want watch it...how can I do?
You need to dismiss third view controller first and then second Viewcontroller. Do the following code when you want to go first view controller.
-(void)goToFirstView{
UIViewController *vc = [self parentViewController];
// UIViewController *vc = [self presentingViewController]; //ios 5 or later
[self dismissModalViewControllerAnimated:NO];
[vc dismissModalViewControllerAnimated:YES];
}
How is the Third modal view being dismissed in the first place? Perhaps by the user touching a 'Done' button? If so, it is in the handler for the button that you want to dismiss both.
You can dismiss both as:
[self dismissModalViewControllerAnimated: YES];
[self.presentingViewController dismissModalViewControllerAnimated: NO];
This happens coz viewDidAppear is called everytime before the view appears so as soon as it appears you dismiss it and it disappears..
I don't think what u are trying to do can be achieved with modalViewControllers...
instead use a navigationController and keep adding your viewcontrollers onto the stack and when you want to goto the First view controller just call
[self.navigationController popToRootViewControllerAnimated:YES];
EDIT:
just thought of it this can be achieved by using delegation.. you make second the delegate of third and as soon you dismiss the thirdviecontroller send the delegate a message.In this message call [self dismissModalViewControllerAnimated:NO];..
and you are done.. (pretty easy if you know delegation.)

With Xcode 4.2 remove a view from navcontroller then push a new view?

So I have a nav controller and I push a view on the screen, a settings view, but I want to remove it out of the hierarchy, so when the new viewcontroller comes onto screen and the back button is pressed it returns it to the main menu.
I tried this:
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:tabBar animated:YES];
But the the viewController is just popped off and the new one is not presented.
You should try to modify the navigationcontrollers view controller array like the following way:
[self.navigationController pushViewController:tabBar animated:YES];
NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[VCs removeObjectAtIndex:[VCs count] - 2];
[self.navigationController setViewControllers: VCs];
Maybe the animation will be broken. If this happens you should move the part with the modification of the navigationcontroller VC array to the presented VC's viewDidAppear method.
The problem is that self is released as soon as you call popViewControllerAnimated:. The push call then sends a message to nil.
You could assign self.navigationController to a local variable first, and use that for pushing and popping instead.
Alternatively, and it's a bit of a hack, but before this, you could call [[self retain] autorelease] to give it an extra retain so that it won't disappear until the end of the run loop.

Resources