I push new view controller like this.
[self.navigationController pushViewController:resultViewController animated:YES];
but in the resultViewController, i do the same thing like
[self.navigationController pushViewController:resultViewController2 animated:YES];
But I'm not sure I'm doing right. Cuz I think the two navigationController instance should be same.
I know that navigationController is a pointer but not sure those two are pointing same thing.
Cuz to manage view stacks, views should be pushed in one navigationController. Am I right?
Then how do I get the top navigationController from resultViewController class?
or is all process automatic somehow? like by setting pushed viewController's navigationController to self(navigationController pointer) when pushViewController method called? So I can just get self.navigationController and push another new viewController?
It's automatic. a navigation controller is the same throughout all the views in it's stack. So calling self.navigationcontroller in any of those views would be a pointer pointing to the same navigation controller
Related
I have UITabbarMoreController which contains a few UINavigationControllers. These UINavigationControllers then contain my custom view controllers.
So the hierarchy looks something like this:
UITabbarController
- UINavigationController
-> my custom UIViewController
- ...(other children of UITabbarController look the same)
From within my custom view controller I call [self parentViewController] which should return a UINavigationController. And this really happens, I really do get a UINavigationController, BUT only when the particular UINavigationController is NOT inside moreNavigationController.
Since I have many children controllers in the tabbar controller, the tabbar controller creates a moreNavigationController. If I open a viewcontroller that is under moreNavigationController and call [self parentViewController] it returns a mysterious object with class UIMoreNavigationController
I really need to get the UINavigationController that is parent of my view controller, not the UIMoreNavigationController. Also, I tried using [self navigationController] with the same result. How can I get reference to the real closest parent of my viewcontroller? Thanks for help in advance!
In short, you can't.
Apple always try to optimise their code. One of their optimisation here is to check whether the ViewController displayed in the list of its UIMoreNavigationController is of type UINavigationController. Because UIMoreNavigationController itself is a UINavigationController it does not want to create another UINavigationController. It's trying to avoid UINavigationController nesting.
When you look at the header file of UIMoreNavigationController you will notice it has a variable
UINavigationController* _originalNavigationController; which is accualy the original UINavigationController that you created and that you want to access. Unfortunetly you cant since UIMoreNavigationController is private.
Workaround (ugly)
Pass the reference of your NavigationController to its children when you push them on it's stack.
I have been using navigation view controller for some time now, and it really does great job. Problem is I don't fully understand it. Maybe some experienced members can shed some light on this topic. I have several questions:
1) Every class that extends UIViewController, has a property navigationController. Apple doc states this -"Only returns a navigation controller if the view controller is in its stack". Does this mean that this property is nil, if this controller is root controller.
2) When using method [self.navigationController pushViewController:nextController animated:YES] nextController is pushed to stack. If you then call self.navigationController inside nextController will navigationController property be nil?
3) Does every navigationController have its own stack, or there is shared stack for all controllers?
4) Finally what happens to items on the stack if you dont pop them, but release navigation controller? Lets say you do push, push, push, and then do release on navigationController. Do these objects stay on the stack or are they destroyed?
1) yes
2) no
3) Every navigationController have its own stack
4) When you push a view controller, navigation controller retains it. When navigation controller is released, then it releases all view controllers in the stack.
I have a project based on Xcode's NavigationController template. This template has a navigationController and a RootViewController.
From inside this RootViewController I push a view and this view pushes a third view. Something like:
NavigationController >> RootViewController >> ViewController 1 >> ViewController 2
Now, from ViewController2 I want to access the navigationController and the navigationController.toolbar.
I know that every viewController has the navigationController property but my question do I have to do something when I push a new viewController so this variable (on the view that is being pushed) will have a valid reference to the correct ViewController or all pushed views will always have a valid reference to the navigationController?
The reason for my question is that I am trying to access the navigationController.toolbar, to make it invisible, and I am having no result.
thanks.
You might want to try -[UINavigationController setToolbarHidden:animated:] to hide the toolbar instead:
[self.navigationController setToolbarHidden:YES animated:YES];
This has always worked for me no matter how deep in the navigation stack my view controller was.
In the entire navigation stack of one UINavigationController object, every view controller's navigationController property has the same value.
The navController is like a box that contains the viewControllers within, with the last one to be pushed shown to the user until it is popped off, when the one below it will come to life again.
This means you can rely on the navController instance always being available from within a controller that was pushed by the navController.
I'm trying to implement a navigation controller with some hierarchical views. I want to use a regular UIViewController to present choices for drilling down, I don't want to use the navigation bar - I want to have my own, custom buttons for returning back up a level.
I see examples like:
[[self navigationController] pushViewController:nextViewController animated:YES];
and my questions are these: Is navigationController a property of all UIViewControllers? Can I refer to self.navigationController regardless of the view that's on the stack? If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewController animated:YES];
Each view I present will need a button to return to the previous view, or to the root view, depending on the situation. I want to create that button in each view controller and control which view in the stack it returns to. Am I on the right track?
Is navigationController a property of all UIViewControllers?
Yes.
Can I refer to self.navigationController regardless of the view that's on the stack?
Every UIViewController on the UINavigationController's stack will return the UINavigationController object when calling navigationController on it.
If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewControllerAnimated:YES];
Yes. popToRootViewControllerAnimated: will take the user to the root UIViewController for the UINavigationController, and you can use [self.navigationController popViewControllerAnimated:YES]; to just pop off the top UIViewController. This last one does the same as tapping the Back UIBarButtonItem.
Am I on the right track?
Yes :)
I have an UIView added in the main window with a controller. On clik of a button on this view I want to load a UINavigationController which will migrate to multiple views pushing them one by one on stack. Now what I want to do is when user reaches at the end of views, in the last view I have a done button. ON clik of this button I want to move back to my first screen unloading the NavigationController from the memory.
What is the best way to do it since popToRootViewController takes you to the first screen of UINavigationController which is my second screen.
You basically want to remove the navigation controllers view, so why cant you just say [navigationController.view removeFromSuperView] ?
One way to do this is to present the navigation controller as a modal view controller, and dismiss it when you're done:
// In the parent controller, when the navigation controller is about to appear:
UINavigationController* navController = [[UINavigationController alloc] init];
[self presentModalViewController:navController animated:YES];
// ... later, in the nav controller, when it's done being used:
[self.parentViewController dismissModalViewControllerAnimated:YES];
[self autorelease]; // goodbye, cruel world (when the ar pool is drained)
A few ideas, in order of desirability
make Controller #1 the root view controller of the stack and then use popToRootViewController. Is there a good reason why you aren't doing this already? Keep in mind you can easily hide the navigation bar from any controller, if that's what you're afraid of.
Add a method called "destroyNavigationStack" or something to main Controller #1 and have a reference to controller #1 in your app delegate. In your Nth view controller, when "done" is hit, get a reference to your app delegate (UIApplication's sharedApplication method), and send View Controller #1 this "destroy" message. There really is no reason to even think about popping view controllers off of the stack since you just want to get rid of the entire stack anyway.
Make ViewController #1 a singleton and call destroyNavigationStack