I have a Navigation Controller with a root table view which has several links. Tapping each link moves to the next view (by pushing it to the navigation controller's stack). But suppose that in that "next view", I have a UIButton that should take me further to another view (by pushing on to the same navigation controller's stack)...
View Controller-->first view-->second view-->third view..........
Now, I can easily access the Navigation Controller when I deal with the first view (and successfully push it to the Navigation Controller's stack) because it has been instantiated in the same file itself. What my real doubt is--How do you access a Navigation Controller in a far off view controller (eg, the third view or fourth view etc)? Please note that I am not using any separate delegate. All the Navigation Bar methods have been implemented in one file and connected to the Navigation Controller via an outlet.
When you push a ViewController onto a NavigationController the ViewController will automatically have it's navigationController property set. This means you can access the same NAvigationController no matter where you are in the stack.
-Update-
navigationController
In every UIViewController you can access that property.
So to in any other UIViewController that has been pushed onto the stack you should be able to just do this:
[self.navigationController pushViewController:othercontroller animated:YES];
Look at the documentation for UIViewController to see what other magic properties you have available.
Related
I have a screen in my iPhone application where i have to load multiple kinds of data simultaneously. To simplify my code, I created separate view and loaded separate view controllers in them to handle each set of data. The blue rectangle on the top has a separate VC and the blue square(ish) below it has another VC that has a navigation controller. All is going well until I suddenly need to push a viewcontroller onto my stack in one VC if user taps on the view that is handled by a different VC.
You could add a property to each "sub" view controller where you could set a UINavigationController. Set the UINavigationController from the "container" view for each "sub" view controller and use this navigation controller instance to push other view controllers.
So I have a Navigation Controller in which for every row select, I push ViewController A. However, ViewController A is another table of items.
So I'm having trouble figuring out how to make ViewController A also act as a navigation controller, in which for ever row I select in ViewController A, I can push a view controller, which, we'll name ViewController B.
I tried to add a Navigation Controller object inside an existing Navigation Controller to no avail.
How will I go about this?
Thanks in advance!
NavigationController has a rootController - in your case this is a some kind of table view controller. When you push view controller A - you use parent Navigation Controller. So you can push as many as you need view controllers via this "parent" navigation controller. Read carefully this section of apple docs
I have an iPhone / iPad application that manages its numerous view controllers via a UINavigationController and UITabViewController. The UINavigationController handles the majority of the user interaction and the UITabViewController handles user settings/preferences.
My app delegate initializes the UINavigationController and pushes the first view controller. Settings (the UITabViewController) can be accessed via a button on the navigation controller's menu bar; the user can return to the main application (the UINavigationController) via a button on the UITabViewController.
My question is: what should I be doing with the UINavigationController (and its stack of view controllers) when I show the UITabViewController and vice-versa? Is there any reason to remove/release/recreate each parent controller as the user switches between the two, or should I be adding/removing each parent controller's view to my app's window?
It seems that the first option would be more mindful of memory/resources, however these benefits might get overshadowed by the processing cost to re-alloc/init the view controllers each time.
Thanks.
You do not have to manage the navigation controller's stack manually. What I would do is present your settings view controller as a modal view. You would do this at the navigation controller level.
Assume that settingsViewController is a property of your main view controller.
self.settingsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal
[self.navigationController presentModalViewController:self.settingsViewController animated:YES];
I assume you mean UITabBarController.
If your navigation view would show up in the tab view for a tab item, I would suggest let the tab bar controller to be the root view controller of your application (and therefore always seen in the app).
If your navigation view is the main view, and the user simply opens up the tab bar view for settings etc. and will return for the navigation view, then modally presenting the tab bar controller is the right way.
In the first case (the navigation controller IN the tab bar controller), you would not care for adding/removing or allocating/releasing views as the UIKit will manage it for you.
In the second case you may create a tab bar controller when you want to show it. presentModalViewController will retain the view controller so you can release it immediately after you send the message. If you want to hold the view controller then you create one when the application is loaded and retain it in the navigation controller (and the navigation controller will always remain in the memory).
Is there a way to unload a UITabBarController when it is popped off the stack of a UINavigationController, so that it is reloaded fresh when pushed back on the stack? I need to clear all of the data in all of the view controllers in the tab bar.
I've tried various methods of iterating through the tab bar's view controllers and setting each to nil, and setting the tab bar's view to nil, and I've also tried sending the didReceiveMemoryWarning message to each of the views.
In my main nib file (MainWindow.xib), I have a UINavigationController and a UITabBarController. Both are wired up to the app delegate as IBOutlets to ease in automatically loading the controllers, especially the tab bar controller.
The nav controller first loads another view as its root view, and when the user taps a button, the UITabBarController is pushed onto the UINavigationController stack.
This all works fine.
Now I want to be able to pop the UITabBarController off the nav controller stack, so the root view of the nav controller is redisplayed, and have it reset all of the data on all the views in the UITabBarController while it is hidden (off the stack).
From one of the view controllers in the tab bar controller, I can call [self.tabBarController.navigationController popToRootViewController:YES] and that works fine.
But how do I unload and reload all of the UITabBarController's views?
You should be able to re-init the view controllers and pass them into your tabBarController's setViewControllers: method.
I'm starting an app where I'd like to have multiple view controllers. Some of the views will be displayed inside of a navigation controller. I can create a navigation controller and then add another instantiated view controller to it. But what I'd like to do, is just instantiate a view controller that has its own view and is the root view controller of a navigation view controller. So when I instantiate the view controller, I'd like for it create a navigation controller and push "self" on to it. When I do it my simulator crashes and the details don't really give a reason. The console does not display anything. Any ideas. My reason for this is to separate out logic without have a view controller that simply creates a navigation controller and then pushes another view controller on it as the root view controller.
I'm not entirely sure if I understand your question correctly. Why would it be preferable if the view controller pushed itself to the navigation controller? I mean, you have to instantiate your view controller at some point in code (either app delegate or another view controller) anyway. Why can't you just create the navigation controller there, instantiate your VC and then push it onto the nav controller? As far as I can see, this doesn't involve creating any additional view controllers.
Anyway, having a view controller decide by itself where it is used (ie. pushed onto), is not best practice. This way you lose the flexibility of using it in other contexts. Always try to couple your components as loosely as possible.