pushViewController only works with animated:YES - iphone

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

Related

self.navigationController pushViewController not adding controller to stack

I am on a viewController called vcA and I do this:
[self.navigationController pushViewController:vcB animated:YES];
and it works. vcB is pushed. Inside vcB's viewDidAppear I do this:
NSArray *controllers = self.navigationController.viewControllers;
and controllers contains just one object, vcA !!!! (what?)
Why is vcB being added to the controllers array? Is there anything that can prevent that from happening?
thanks.
I banged my head on the wall for a week to solve this problem involving pushing a view controller on the navigation controller stack.
The problem was this: I was on a navigation controlled based app and I had 3 view controllers, vA, vB and vC.
I was pushing vB from vA using this:
[self.navigationController pushViewController:vB animated:YES];
it worked, but, when I tried to push vC from vB, the self.navigationController property of vB was nil and vB was not on the navigation controller stack. What? Yes, I pushed vB on the navigation stack and vB was not being added to it, but even so, vB was showing correctly.
I discovered that inside vB's viewDidLoad, self.navigationController was not nil and that vB was on the navigation controller stack, but as soon as vB's viewDidLoad ended, vB was removed from the navigation controller stack and its navigationController property was set to nil. At that time, vB was a kind of ghost control, outside the navigation controller stack.
I don't need to mention that from vB I was unable to get back to vA or to push vC.
How that happen?
simple, I was pushing vB from inside a block... something like:
void (^ block1)() = ^(){
[self.navigationController pushViewController:vB animated:YES];
};
What was happening because a UIKit function (the push thing) was being executed in a block that was probably running on a thread that was not the main thread.
The resolution to that was to wrap the push in a dispatch to the main thread, using this:
void (^ block1)() = ^(){
dispatch_async(dispatch_get_main_queue(),
^{
[self.navigationController pushViewController:vB animated:YES];
});
};
The app had another minor problem with a wrong outlet connected to a viewController, but this was the main problem. This is the kind of problem hard to spot, for being subtle. I discovered the problem when I added another button to the navigation bar and that button did not appear. So, I suspect that something involving UIKit was happening, or not happening in that case. The big problem here is that this code was not crashing, no error message, nothing, just working bad, but working.
I believe this has to do with the timing of your checking. During the pushViewController method, the next view controller's viewWillAppear: method (and others, such as the one you're checking) are in the process of getting called. I believe after all those method are called, the view controller is considered on the navigation stack AFTER the animation is done completing.

Go to first view controller in app

I need to go to the first view in my app. I have a few views pushed onto the stack then a modal navigation controller and more views pushed onto that.
The problem I'm having is that using [[self navigationController] popToRootViewControllerAnimated:YES]; only goes back to the first view in the modal stack.
And I can't get [[self navigationController] popToViewController:.. to work because the true first view controller isn't accesible with [[self navigationController] viewControllers].
Any ideas on how to accomplish this? Thanks.
Do this:
[[self navigationController] dismissModalViewControllerAnimated:YES];
That will get you back to the VC that modally presented the navigation controller. Getting farther back after that depend on how you pushed those "few views" before the navigation controller.
Edit - explanation to get to the deepest root...
It sounds like those "few views" are on another, underlying navigation controller's stack. This can be a little tricky, because the clean way to get farther back in that stack is to have that underlying navigation controller pop to it's own root. But how can it know that the modal VC on top of it is done?
Let's call the view controller that did the modal presentation of second navigation controller VC_a. It's a modally presented navigation controller whose topmost VC is VC_b. How can VC_a know to pop to it's navigation root when VC_b modally dismisses itself?
The good answer (usually) is that VC_b decided to dismiss itself for a reason - some condition in your app/model changed to make it decide to be done.
We want VC_a to detect this condition, too. When VC_b gets dismissed, and VC_a gets a viewWillAppear message because it's about to be uncovered:
// VC_a.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (/* some app condition that's true when VC_b is done */) {
// I must be appearing because VC_b is done, and I'm being uncovered
// That means I'm done, too. So pop...
[self.navigationController popToRootViewControllerAnimated:NO];
} else {
// I must be appearing for the normal reason, because I was just pushed onto the stack
}
}
You need to do it by using the delegation pattern. Specifically, by creating a protocol that implements the delegate's respondsToSelector method.
See this post for complete details. It should be almost exactly what you are looking for. I had to do something similar, except I only needed to pop one view off the navigation stack instead of using popToRootViewControllerAnimated:.
For iOS6...
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
In AppDelegate.m class create method with bellow flow...
-(void)MethodName{//your method name
YourViewController *objViewController = [[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil] autorelease]; ///define your viewcontroller name like "FirstViewController"
UINavigationController *yourNavigationController = [[[UINavigationController alloc] initWithRootViewController:objViewController] autorelease];
self.window.rootViewController = yourNavigationController;
}
When you want redirect on firstview just call this method from appdelegate object....

nested push animation can result in corrupted navigation bar

in one of my viewcontroller, lets say A, I have the following:
OnbViewController *on = [[OnbViewController alloc] initWithNibName:nibName bundle:nil];
onboardingTutorial.delegate_ = self;
self.test = on
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:self.test]
[self presentModalViewController:nController]
[nController release];
then inside OnbViewController I have pushed to the navigationController:
[self.navigationController pushViewController:someViewController];
then in didSelectRowForIndexPath: I called a delegate, which is A, inside the delegate function I called I tried to push again by doing:
[self.test pushViewController:someOtherViewController];
and then this gives me that error:
nested push animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
Unbalanced calls to begin/end appearance transitions for <someOtherViewController: 0x6f942e0>.
any idea?
Rather convoluted calling sequence, but the first issue I see is that it looks like you're calling pushViewController on an OnbViewController, not on its navigationController. Maybe changing to [self.test.navigationController pushViewController:someOtherViewController]; will suffice?
Edit: in looking further at this, I note the "animation" in "nested push animation". Is onbViewController pushing to someViewController during its viewWillAppear or somewhere early like that? Maybe skipping the second animation will work?
It's a real tangle of pointers getting created there. self.test is the root vc of a navigation controller (nController). You present the nController modally, and then push other view controllers onto it's vc stack.
Then, while that stack is still there, you push onto self.test, which is like pushing onto the root again.
Let the VC that's getting the didSelectRow message do the push. You can still tell the delegate that it happened.

Re-Presenting Modal View Brings to last VC instead of first

I have a Navigation Controller that is presented modally with 4 views in the stack. The final view has a done button that dismisses the modal view. When I then present the modal view again, it automatically goes to that last view instead of the first one. I added a line to pop to first view after dismissed but it adds a weird animation whether I set it to YES or NO. Maybe I'm doing it wrong?
- (void)dismissModalView
{
[self dismissModalViewControllerAnimated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -4)] animated:YES];
}
Update:
This is the method used to present the modal view/navcontroller
- (void)showModalView
{
self.optionsNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentModalViewController:self.optionsNavController animated:YES];
}
No, it looks right. If you want to retain the state of the Navigation controller then don't present it modally as it will get deallocated when you dismiss the view.
Modal views are usually used to present information that only needs to be shown briefly without maintaining the state of the view (i.e. about page, login page, settings page, etc).
Excuse me if I'm oversimplifying your issue, but if your need is to pop straight back to the first view controller, perhaps you could give the popToRootViewControllerAnimated: method a try instead of popToViewController:.
I'm not sure what the problem with the dismiss code you posted is, but the following should work:
- (void)showModalView
{
self.optionsNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.optionsNavController popToRootViewControllerAnimated:NO];
[self.navigationController presentModalViewController:self.optionsNavController animated:YES];
}
Also, viewDidLoad is intended to only be called once every time the view is loaded, viewWillAppear a

How to jump to a specific view?

I have 3 views (xib'd) the third view opens a modal view (also xib'd).
My goal is to dispose the modal view and jump on the view #1.
I used the following code but it does nothing.
self.statusView = [[StatusViewController alloc] initWithNibName:#"StatusViewController" bundle:nil];
[self.navigationController popToViewController:self.statusView animated:YES];
[self.navigationController popToViewController:
I also tried the following, same result.
[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
I'm going crazy...
statusView has an accessor regularly synthetized and it represents the view that I want to jump to.
It is not entirely clear how your views are set up with respect to each other, based on what you've said so far.
I'm guessing you have a navigation controller, and 3 view controllers that are displayed on the navigation stack.
If that's the case, and you want to pop back by two screens at once (from #3 to #1, skipping #2), then you need a pointer to the view controller for #1 (not the view itself). It looks as if the first popViewController: method call in your question is sending in a view.
Sample code to pop to the first view controller:
UINavigationController* navController = self.navigationController;
UIViewController* controller = [navController.viewControllers objectAtIndex:0];
[navController popToViewController:controller animated:YES];
If you've tried this, and it doesn't work, a few things might be going wrong:
Maybe self.navigationController isn't actually the right object.
Maybe the view you expect isn't actually view #0 on the navigation stack.
Maybe the navigation controller you're working with isn't currently visible.
Here are some further steps you can take to test these hypotheses:
When you first allocate the navigation controller you want to work with, call NSLog(#"Nav controller is at %p", navController); and in this code add a call to NSLog(#"Now my navController is at %p", navController); and check that the addresses match.
If the nav controller is the right one, print out the current navigation stack; something like this (which assumes each view controller has a different class name):
for (UIViewController* viewController in navController.viewControllers) {
NSLog(#"%s", class_getName([viewController class]));
}
Do something visual to the navigation controller you think is visible to make sure it actually is. For example [navController.visibleViewController.view addSubview:aColorFulView]; where aColorFulView is some visually obvious UIView.
-popToViewController lets you pop controllers OFF the stack. What you want to do is push new viewControllers ONTO the stack, so use:
[self.navigationController pushViewController: self.statusView animated: YES];