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

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.

Related

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.

TabBarController with NavigationController

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.

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.

How to build navigation controller from existing view controllers drew in storyboard?

I have built some view controllers in storyboard like in the picture below
I already implemented the data inside them, modal segue is used for transitions in between. Now I just realise when I push "back" button, previous view won't be properly loaded. I figure I should switch to navigation controller and add those controllers in stack instead. But I don't know how to go from where I am now.
I think I should make changes programmatically because I found building navigation controller in storyboard won't have much variation in UI design (at least I don't know how to implement existing pages in that way). So what should I do to implement programmatically? Please help me, thanks!
Select Category View Controller and go to menu: Editor > Embed In > Navigation Controller. Then change segues style from Modal to Push.

Switching UINavigationViewController to UITabBarController with Segues

I am changing my app to use UITabBarController instead of UINavigationViewController. I replaced controllers accordingly and app launches with tabs successfully. Later in the code I came across some difficulties using segues where lines like
[self performSegueWithIdentifier:#"ImageViewController" sender:self];
won't work producing error
"Push segues can only be used when the source controller is managed by an instance of UINavigationController"
When I embedded UINavigationController into the initial FirstViewController, line above worked fine. I need that line to switch between views and pass some data to another view controller.
The problem is that using segue identifier to go to SecondViewController hides TabBar navigation (unless I wrap SecondViewController in UINavigationController again and so on). That is not the solution.
Question: How to use UITabBarController and still take advantage of segues while keeping tab navigation visible?
"Push segues can only be used when the source controller is managed by an instance of UINavigationController"
Sounds to me like you are using a push segue somewhere outside a navigation controller. Try to select all "segue bubbles" in interface builder that are not inside a navigation controller:
and check the segue's style in the right pane:
If the style is set to the value push as shown above change it to modal or custom:
There is something wrong with your connections. If you are using interface builder (storyboard) then your connections should look like this. See pic
Notice you are missing UINavigation Title headers. Also the gray area below on each view controller means that space is reserved for images for UITabBarController
Another problem is your UIToolBar. Looks like its sitting on top of where UITabBar will be displayed. That may/may not get your app rejected by apple since you already have a UINavigationController and UITabBarController. If you need more buttons/options on that page make them UIButtons instead.

Resources