How to use a UITabBar with separate storyboards in iOS8/Swift? - ios

So basically I have several storyboards that contain "reusable" content (like a form and some other things). I have another storyboard that has a UITabBarController with one tab that contains a TableViewController and a detail view, a second tab that opens a Settings view, and potentially a third button that opens the form (referenced earlier in my question) modally. My question is...how can I create this third tab within my Tab Bar Controller storyboard since the view that it opens resides in a separate storyboard (as in I can't drag a segue like is the normal process)?

You can instantiate a view controller programmatically from a storyboard by using the instantiateViewControllerWithIdentifier method of the storyboard that defines that view controller.
To get an instance of your form, you may do something along the lines of this:
let storyboard = UIStoryboard(name: "YourFormStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("formViewController") as! YourFormClass
You then populate the tab bar controller with its root views as you always have, laid out here in the Apple docs:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/

Related

How to use UItabbar on a UIviewcontroller?

currently I have a main Viewcontroller. Now I wish to implement the UItabbar to switch between few controllers. The problem now is this main view controller is just a normal UIviewcontroller, how can I implement the UItabbar and switch vc with selecting the tabbar item? Thanks all
if you are using storyboard
Get uitabbarcontroller-and link between it and the viewcontroller(by right click and drag to the view controller then select view controller from the drop menu)
Here is a tutorial
https://m.youtube.com/watch?v=nGx3MZM460c
If you want to create TabBar into your project you have to write it into AppDelegate:
let nav1 = UINavigationController(rootViewController: ViewController()) // ViewController inside TabBar
nav1.tabBarItem.title = "Title of VC"
nav1.tabBarItem.image = UIImage(named: "name_of_image")
let tabBarVC = UITabBarController()
tabBarVC.viewControllers = [nav1] // All VCs, what you want in TabBar
window?.rootViewController = tabBarVC
So I hope it helps you. If you write code programmatically without the storyboard.
through storyboard you can add tabBarcontroller to menuviewcontroller
intially my storyboard is like this
Drag from tabbarcontroller to menuviewcontroller you will get a pop up like this
Select relationship Segue "viewcontroller" and your storyboard will look like this
I don't know if its been made simpler since these answers were written. But it seems quite straightforward now - at least with Xcode 11.5.
So to add a tab bar to an existing Storyboard that's already got a UIViewController:
Drop a 'container view' onto your existing view. Pin it to the safe area. It doesn't need to cover the whole thing - eg. if you want a non-tabbed bit at the top. But it makes sense for it to extend it down to the bottom of the view - otherwise your tabs end up looking very non-standard!
When you dropped the container view, that will have created a new UIViewController for the container's contents. Delete it.
Now drop a Tab Bar Controller on the storyboard - eg. next to the main view controller's view. This creates three new view controllers - the tab bar controller, plus one for each of the two tab items it creates by default.
Now the crucial bit - we want to make the container view contain the tabbed views...
Select the Tab Bar Controller, select the Connections inspector, and drag from the 'Embed' presenting segue to the container on your main view.
Once you've dragged, a menu will pop up by the cursor with a single item - 'viewDidLoad'. Make sure you select this. It can be a bit fiddly to do for some reason - but once you select it correctly you'll see the link from 'Embed' to 'Container View viewDidLoad' in the Tab View Controller's connections inspector.
Voila! - you're done!

Bar item button not displayed at runtime but visible at design time in a modal view

I have being trying different things and looking around for a while without finding an answer to my problem. Maybe I'm doing something fundamentally wrong.
The sample application consists of:
A first view controller that displays a second view controller using a segue. This works fine.
A second view controller, in which I have simulated the display of a third view programmatically, which contains a bar item button (named "Done") that I would like to display.
The bar item button in the third view controller is not displayed at runtime but is displayed in IntefaceBuidler at design time.
This third view controller needs to be displayed modal.
What I'm doing wrong to display this bar item button?
A sample project illustrating the problem is available here.
Below a screen capture of the bar item button at design time:
Below a screen capture of the bar item button not showing at design time:
PS:
Please disregard the "Unknown class ThirdViewControlller in Interface Builder file.", since the ThirdViewController is displayed fine at runtime. Also, the "Done" button in the middle of the view works fine.
In SecondViewController you need to push the third onto the navigation controller stack like so:
self.navigationController?.pushViewController(thirdViewController, animated: true)
You are currently presenting it as a modal. Also, you've unnecessarily added a second UINavigationController to your storyboard (for the third view controller)
If you want to present a modal, then you'd need to embed the controller in a navigation controller:
let navController = UINavigationController(rootViewController: thirdViewController)
self.present(navController, animated: false)
If you prefer to keep this within the storyboard, then you need to provide a identifier for the UINavigationController and insatiate that in your function.
The above button is a navigation bar item that will only be displayed on the navigation bar . For achieving your desired result , you first have to embed the navigation controller at least in your second viewcontroller and then you should do a push segue rather than modal . Navigation controller can be added by
whith your second viewcontroller selected go to Editor\Embed In\Navigation Controller
for pushing the viewcontroller programatically onto user navigation controller's stack use
self.navigationController?.pushViewController(nextViewController, animated: true)

Using Storyboard reference for TabBar is not displaying the TabBar item image and title

I am creating an TabBar controller app via Storyboard. I am using Storyboard reference object to link another storyboard for the 3rd tab item. It is working correctly but i didn't get the title and image for that Tab item. I can see image and title in the storyboard but while running I am unable to see those in the simulator.
Main Storyboard:
Second Storyboard:
App:
Thanks in advance!
Drag Tab Bar Item from the Object Library to UIViewController in the second Storyboard.
Fill up Title and select Image in Bar Item in the second Storyboard.
You should programatically instantiated that storyboard and then populate the tab bar controller with its root views as you always have, laid out here in the Apple documentation.
You can instantiated storyboard like,
let storyboard = UIStoryboard(name: "YourFormStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("formViewController") as! YourFormClass
so, instantiate storyboard like this and manipulate your tabbar controller programatically.
It is better to not use multiple storyboard if not required.
Hope this will help :)
here you are Embed a nvigation controller between tab bar controller and your view controller . so now your view conterller in your navigation controller not in tab bar controller.
if you want to show tab bar image and item then give segue from tab bar controller to your view controller :)

Segue from one ViewController to another ViewController inside a Tab Bar Controller?

In the interface builder I have a UITabBarController and it is set as the initial view controller. From the tab bar controller, I have linked three independent ViewControllers; two UIViewController's and one UITableViewController. I have embedded all three of these views inside UINavigationController's as each of these views will eventually segue to a new view.
Interface Builder
Problem:
I now want to link one of the UIViewController's to the UITableViewController using a button to segue to the table view. This way I can pass information, i.e. func prepareForSegue(), to the table view. I want to maintain the tab bar controller at the bottom, however I do not want to have the ability to go back to the previous UIViewController from the current UITableViewController via a UIBarButtonItem at the the top of the view; That is what the tab bar at the bottom is for.
However every time I segue "Show" the table view (it is actually a segue to the table views navigation controller), the navigation bars at the top and the bottom of the table view disappear. Is there anyway to prevent this from happening?
I have also tried segue "Show" directly to the table view, in which case the tab bar is visible, but then it displays a "back" button at the top of the view to segue back to the sending UIViewController. I am hesitant about accepting a solution that would just hide the back button, because I feel I will run into problems down the road when I want to navigate to a detail view from the table view itself, since I would be bypassing the UITableViewController's UINavigationController.
Any solutions would be greatly appreciated. I have been trying to solve this problem for hours and I'm about to put my head through my computer screen. Also I thought about just using tabBarController?.selectedIndex on the button click to shift to the table view, and then passing the information using NSUserDefaults, but this is out of the question since I would be passing a custom object, and would have to encode and decode every custom field.
I you use a segue to get to it, as you say, you will still be using the UIViewController's UINavigationController which seems a bit messy. So I actually think selectedIndex is probably the best way to go as once you change to the UITableViewController you'll be in the correct navigation stack.
Instead of using NSUserDefaults, why not just reference the UITableView itself from the UIViewController, set the values you want, and then swap to it using self.tabBarController.selectedIndex.
So for your scenario above it, assuming the UITableViewContollrer is the third view in the UITabBarController, you would do something like the following:
Pass whatever you want into the UITabBarController by setting some pre-defined var in it. For example, if there was a String called saveMe in the UITableViewController, then do the following in the UIViewController:
let navController = self.tabBarController?.viewControllers![2] as! UINavigationController
let tableViewController = navController.viewControllers.first as! JarListTableViewController
tableViewController.saveMe = "Saved string here"
Swap to the UITableViewController using:
self.tabBarController?.selectedIndex = 2
The only issue with this is using selectedIndex won't perform a transition animation but not sure if you need this. This link could help if you do.

Xcode IOS transition views with a tab view controller

I am developing an IOS app using Swift and have a tab view controller. I start the app at the SecondViewController to allow the user to insert their information, upon clicking save I would like to programmatically transition to a different view. When I use the following code, the view transitions however the tab bar disappears.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let home = storyBoard.instantiateViewControllerWithIdentifier("home") as FirstViewController
self.presentViewController(home, animated:true, completion:nil)
Should I not be presenting the view and just using a different form like segue or something?
Again this is a tab bar controller.
Thanks
Using presentViewController method, your particular view controller get's displayed modally above your current tab bar and navigation stack.
To keep your tab bar visible you either have to push your new view controller on the current navigation stack by calling the method pushViewController(..) on your current navigation controller, or - this is actually the right thing in your particular situatuin, assuming that you defined you FirstViewController in your storyboard and connected it to the first tab in your UITabBarController— you can simply tell your tabBarController to select the particular segmentedIndex (in this case 0).
Something like this should help:
self.tabBarController.selectedIndex = 0

Resources