TabBarController with NavigationController - ios

I have a TabBarController with 3 tabs set up in my storyboard. I want to have each tab have its own navigation controller. However, I don't want to embed each one in a nav controller, cluttering the storyboard and then having to style the navigation each time. Is there any way to do this programatically? In other words each time a tab is tapped, the resulting view controller will be set as the root of one existing navigation controller?

I don't think that there is a way to 'set' the rootViewController every time, but you can do 3 navigationControllers in your storyBoard and build a subclass of UINavigationController. That way you don't have to set it every time. Subclassing UINavigationController has become possible since iOS 6 (See 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.

Related

Putting UITabbarController inside UINavigationController. What problem may occur?

I have a stack like below hierarchy (I'm using programatic way, so I done it)
UINavigationController -> UITabbarController -> UInavigationController -> UIViewController
-> UInavigationController -> UIViewController
As Apple Docs say for pushViewController:
The view controller to push onto the stack. This object cannot be a tab bar controller
I need to know If this has a known bug or will CERTAINLY causes a bug.
I really searched a lot for similar posts but none of them give me an acceptable answer.
Using UITabBarController with UINavigationController - Swift 3
Can you push a UITabBarController inside an UINavigationController
From Apple:
Before creating a tab bar interface, you need to decide how you intend
to use a tab bar interface. Because it imposes an overarching
organization on your data,you should use one only in these specific
ways:
Install it directly as a window’s root view controller.
Install it as
one of the two view controllers in a split view interface. (iPad only)
Present it modally from another view controller.
Display it from a
popover. (iPad only)
Installing a tab bar interface in your app’s main
window is by far the most common way to use it.
In such a scenario,
the tab bar interface provides the fundamental organizing principle
for your app’s data, with each tab leading the user to a distinct part
of the app. You can use tab bar controllers by themselves or in
conjunction with other view controllers to create even more
sophisticated interfaces. For more information, see Combined View
Controller Interfaces.
It is also possible to present a tab bar controller modally if a very
specific need makes doing so worthwhile. For example, you could
present a tab bar controller modally in order to edit some complex
data set that had several distinct sets of options. Because a modal
view fills all or most of the screen (depending on the device), the
presence of the tab bar would simply reflect the choices available for
viewing or editing the modally presented data. Avoid using a tab bar
in this way if a simpler design approach is available.

Storyboards and UITabBarControllers

I'm using Storyboards for the first time. It's mostly going okay, but one situation is very unclear:
I'm using a Tab Bar Controller as my Initial View Controller. I have three tabs, and they're each an instance of the same view controller class, the only difference being that I want to pass in a different array to each instance, to display different data.
My thought was I could use prepareForSegue: in the UITabBarController instance to pass the proper array to each destination view controller…but maybe UITabBarController isn't using segues to display each view controller in a tab, because there's no way in IB to specify a Storyboard ID for the "segue" to the tab contents. (If I select the segue that connects the Tab Controller to the child Controller, IB just says "Not Applicable" in the customizer area.)
So my question is: how can I handle this situation in IB?
UITabBarController uses segues, but it is a special kind of segues. Select a UITabBarController in the IB, and open the Connections inspector in the Utilities area. You'll see that there are Presenting Segues in the bottom (those are handled by prepareForSegue) and Triggered Segues, which include viewControllers segues. Those are the segues to tabs inside a tab bar. They are not really segues, they are more like references.
In order to use one class for three different view controllers as tabs in IB, you should add three empty (or not) view controllers as tabs and specify their class using the Identity Inspector in the Utilities area. The class can be the same for all three.

How to link a separate storyboard to each particular tab in uitabbarcontroller?

I have an app which has 5 major user flows..each flow is a few screens linking to each other...so each flow warranties its own storyboard. Each storyboard starts with a custom view controller that is embedded in a navigation controller. So far so good.
Now all of this is "stitched" together via a UITabBarController. This is the most default UI design ever known to iOS.
But turns out I don't really know how to link from tabbarcontroller, which is in its own storyboard (that is set as the main one on code project) to any of the other storyboards.
This problem looks so! simple, so I think I am missing something utterly obvious, but I just can't figure out how to do it.
So how do I link from tab bar controller in storyboard 1 to the initial view controller in storyboard 2 when a tab is tapped?
You should do this in code. You can have the tab bar controller (tbc for short) and the controller in the first tab in the app's main storyboard, and in the app delegate, instantiate the other controllers using instantiateInitialViewController. Create a mutable array by copying the tbc's viewController array, add the other controllers you instantiated to it, and then set that array as the tbc's viewControllers array.
You have to add your viewcontroller programmatically in tabbar.

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.

Use of differenct view controllers

i'm curious about what's the best way to plan the controllers for my app.
i want my main screen to have 3 button.
1) should open a nav controller with details view
2) should open a controller with other buttons that lead to others controllers
3) should open a tab bar with 2 pages ( or eventually use a switch to change page instead of the tab bar)
this is the schema of what i want
http://i59.tinypic.com/2rrvrd4.png
Is it a correct schema or i should use my controllers differently? will apple reject an apple with such schema?
thanks
As #Fogmeister pointed out in the comments, going for a UITabBarController as the main interface for your app actually seems to be a more appropriate solution here.
However, you can go with the interface that you described, but then you should keep in mind that with your current setup, you are not only using UINavigationController in the first case, but your whole navigation system is still built upon UINavigationController in the following way:
Your app has one instance of UINavigationController.
Your initial UIViewController (the one with the three buttons), is the rootViewController of your UINavigationController.
You can navigate to the other view controllers using [self.navigationController pushViewController:newViewController] (or performSegue if you prefer using Storyboards).
In the case of your third view controller, you are pushing a UITabBarController onto the navigation controller's view controller stack, this UITabBarController needs to be initialized with the two view controllers that it is going to display before it gets pushed onto the stack.

Resources