How to conditionally set root view controller for a Navigation Controller - ios

I have a navigation controller which should have a different root depending on the state of my App.
How can i model this within Interface Builder? I've dragged out a UINavigationController to my Storyboard. Now I need to make the connections to the two view controllers.
Sadly I can only set one as root view controller. Do i need to subclass the Navigation Controller and based on the state reset the root view controller?
What is the best practise for this kind of problem? It would be nice to visualize this relationship within Interface Builder.
Thanks in advance for any help.

As it is the rootViewController you want to change conditionally, you could instead have two different navigationControllers, each with the one of the two different viewControllers as its rootViewController.
You could then present the appropriate navigationController based on your condition.

Have two segues (one to viewcontrollerA and the other to viewcontrollerB). The your Navigation Controller would determine the App state and call performSegue to move the user to the correct view controller.

Related

How to design a mechanism to manage viewcontrollers transitions, like route

I want to design a routing mechanism for management view controller transitions in objective-c.
Two view controllers, without reference to the other side of the pointer, the transition is completely controlled by the route.
How to achieve the route?
Thanks for your help.
I hope you think like this.
Route = Segue (In Xcode)
Take Two View controller
Make 1st Embed in Navigation controller (Which is must be initial View Controller)
Put One Button in 1st VC
Press Ctrl + Drag Mouse from button to 2nd VC (One popup will be show, then select Push)
Run Project
Press Button
Lol
You can use Storyboard's segues. Create view controllers and segues between them. You can use Push Segue or Model Segue. Push Segue will let you pop next view controller too, and you will have to embed your first view controller into navigation controller. For Model Segue, you can move from one view controller to another but to return to previous view controller, you will have to create another segue.
I think I find the approach to answer this question。 Here is the similar project.

Difference between navigation controller and viewcontroller?

Difference between navigation controller and viewcontroller?
I mean how can we decide when to use navigation controller or a normal view controller?
Just my two cents:
A UIViewController represents a single view and you can put buttons in this view controller to segue to another UIViewController. If you want to segue back to the first UIViewController, you will have to worry about putting a button in the second view controller that leads back to the first. If you are drilling down into view controllers, this can be tedious having to remember to give the user a way back to a previous view controller.
A UINavigationController does a lot of this tedious work for you. As mentioned, it contains a stack of UIViewControllers. It will create a navigation bar at the top that will allow you to easily go back up the hierarchy of view controllers.
In short, if you have a hierarchy of view controllers that you want the user to easily navigate around, inbed your UIViewControllers into a UINavigation controller.
UINavigation Controller is a combination of 2 or more view controllers,those are connected through "segue" feature of "Ios". Benefit of using Navigation Controller is that we can navigate between different screens easily with default "Back" button on each screen . We don't need to give any individual button to move back onto previous screen.
Whereas a ViewController provides a single screen & we can connect more screen using "segue" but we also have to design a "Back" button to navigate onto previous screen.
We should use Navigation Controller , in case where one option resides into another one.Like in an iPhone settings ->Mobile Data Options->Voice->4G or 3G or 2G. It's a hierarchy of menus so here navigation Controller is better option than using UIController.
We should use UiController with "segue " , in case where
we have to choose one option among multiple.Like -
Photos ->There are many folders in which , any one is selected, that are Favourites or People or Places .
Here's a very brief, high-level overview.
Whereas a UIViewController can be thought of as representing a single 'screen', UINavigationController, as the name implies, is used as a means of being able to navigate multiple 'screens'.
From the documentation:
The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but in iOS 6 and later you may subclass to customize the class behavior.
Please see the rest of the UINavigationController documentation here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html
Okay, Thank you everyone for helping me to find out a clear answer on this.
Navigation Controller consists of navigation bar and tool bar to move in and out from view controllers present in navigation stack.Therefore there can be many view controllers in Navigation Controller.
In view controller we don't have this facility and it represents a single screen view.
Please correct me If I am wrong.
See the Navigation Controller discussion in the View Controller Catalog.
Bottom line, a navigation controller actually is a view controller, but it just happens to be one that presents and navigates between other view controllers.

How do I handle a view controller in my Storyboard with two distinct layouts?

I have a UIViewController in my Storyboard that can have one of two layouts, depending on which setting the user chose.
One uses a UITableView to display the information, while the other uses UICollectionView.
In code, I'd simply set up the view controller depending on the setting, but I don't see how I would do this with a Storyboard. How do I handle the multiple layouts in a Storyboard environment? (Worth mentioning this view controller is embedded in a navigation controller.)
This situation is no different than any time you have to make a choice in an app as to which controller should be shown next. Have two different navigation controllers (with storyboard identifiers) set up in the storyboard, one whose root view controller has the table view, and the other with the collection view. After the user makes a choice, instantiate the correct navigation controller, and segue to it, or make it the window's root view controller.

How to model different initial screen on app launch depending upon state with storyboards?

Whenever my app launches one of 3 possible screens will be displayed depending upon the state.
I was going to model this in a storyboard by having the starting view controller as a navigation view controller, then have a segue from it to each of the three other VCs.
Then on app launch the appropriate VC would get pushed depending upon the app's stored state.
However IB will not permit me to make multiple connections from the navigation controller.
If I make an initial segue from the NVC to any of the VCs, say A. Then when I attempt to add another segue from the NVC to another of the VC, B, then instead IB creates a connection between A and B.
First of all is the approach of having a Navigation Controller as a starting RVC and pushing different VCs onto depending on state an incorrect approach? If not, then how to model it with storyboards? Why will IB not let me drag and drop the segue to the other controller?
I don't think the navigation controller is the way to go. IB won't let you connect more than one, because that first connection is the root view controller, and there is only one of those.
There a many different ways you could get what you want depending on where you go after one of those 3 initial views. Do they all go to the same view next? All different view? Some the same, some different? It's hard to advise you without more detail.
You could start off with a tab bar controller, with the tab bar hidden, if you don't want that look. Or, you could have logic in the viewDidAppear method of a common initial controller, and have it switch the root view controller of the window to on of your three screens.

How to point two UINavigationControllers to one view controller using storyboards?

I am attempting to connect two UINavigationControllers, representing different tabs in a tab bar application, towards a single UICollectionViewController. The collection view controller will be populated by different data based on which navigation controller is the parent. When both navigation controllers are connected, navigating to the second tab displays a black screen and the viewWillAppear() code in the controller for the collection view does not fire. When navigating to the first tab, everything fires and is displayed correctly with data populated.
The storyboard setup looks like this:
The navigation controllers are connected to the collection view controller as seen here:
If I disconnect either one of the two navigation controller connections, the remaining connection to the collection view works and collection items are displayed as expected.
Is this the correct way to reuse the collection view? Is there something missing from how I am connecting the two navigation controllers with segues to the single UICollectionView?
I see this is almost a year old, so I'm not sure if you found an answer or not, but I thought I'd help answer if needed. Without knowing too much of your project, can I ask why you want a tabBarController to point both of its view controllers to the same one? Makes me wonder why tabBarController is necessary.
Setting up a test project, I witnessed the same you did. The only thing I can think of is that when a view controller is instantiated via the Storyboard, it creates an instance of it and is stored in the tabBarController's viewControllers[i] property, which the other tab may not have access to in the hierarchy. It would be best to just copy the view controller object from the Storyboard and paste it onto another area of the storyboard, and have the tabBarController instantiate a new instance of the same view controller object. That way, in your viewDidLoad method, you can tell what the selectedIndex is, and know how to load data, alter interface, etc. Copying/pasting the Storyboard view controller should keep intact any outlets/actions you've set up.
Hope that helps...

Resources