How can I present a UITabBarController in Swift - swift

I have a story boar set up with a view that has a button. When I click that button I should go to a custom UITabBarController but all that is displayed is an empty Tab Bar.
let tabBar = TabViewController()
self.presentViewController(tabBar, animated: true, completion: nil)
In the story board I have set the class of the TabBarController to be a TabViewController. What am I missing?

You need to give the TabViewController a storyboard ID in Interface Builder and get access to it via:
if let tabViewController = storyboard.instantiateViewControllerWithIdentifier("TheAssignedID") as? TabViewController {
presentViewController(tabViewController, animated: true, completion: nil)
}
The code you're using is just creating a new instance of the TabViewController class. Not accessing the prototype that you've defined in Interface Builder.

Related

How to present navigation controller with tab bar controller

I have navigation controller and tab bar controller. TBC is embeded in NC. When I present VC from another VC, I see only NC and not TBC there. I want them both to be presented. What should I do?
Here is my code:
let mainAreaVC = self.storyboard?.instantiateViewController(withIdentifier: "MainAreaVC") as! MainAreaVC
let mainAreaVCe = UINavigationController(rootViewController: mainAreaVC)
self.present(mainAreaVCe, animated: true, completion: nil)
If you want to show MainAreaVC with both NavigationController and TabBarcontroller then you need to present the UITabBarController instead of MainAreaVC. So in storyboard set the Storyboard Id for your TabBarController something like TabbarVC or what ever you want then use it wit instantiateViewController to get UITabBarController.
let tabbarVC = self.storyboard?.instantiateViewController(withIdentifier: "TabbarVC") as! UITabBarController
self.present(tabbarVC, animated: true, completion: nil)

Go to a specific UIViewController

I have a RootViewController which is in a navigationController. The RootViewController adds a new navigationController in a view container, that has several UIControllers, the last one is PersonalInfoViewController.
From personalInfoViewController I want to go back to RootViewController. My RootviewController is not the view.window.rootViewcontroller
from any view controller of personalInfoViewController Go back
self.view.window.rootViewController?.dismiss(animated: true, completion: nil)
but you have to change Navigation like this image
let navController = UINavigationController(rootViewController: personalInfoViewController())
rootViewController.present(navController, animated: true, completion: nil)

How can I add a segue identifier to a programmatically modal transition?

I have a controller which is in the Main storyboard. When I click on a button, I call the displayBorneDetailsAction() action, it presents a modal view from another storyboard.
I would add a Segue identifier when I present my modal to pass data from my main view controller to my modal view controller (with prepareForSegue), but I don't know how to do it.
I tried to use performSegue(withIdentifier:), but it doesn't present the modal in the same way.
#IBAction func displayBorneDetailsAction(_ sender: Any) {
// open the modal of a borne
let storyboard : UIStoryboard = UIStoryboard(name: "Borne", bundle: nil)
let vc: BorneVC = storyboard.instantiateViewController(withIdentifier: "BorneVC") as! BorneVC
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
navigationController.edgesForExtendedLayout = []
self.present(navigationController, animated: true, completion: nil)
}
You cannot add an identifier to a programmatical segue, since you are able to access the instance of the controller that is being presented. Simply do anything you want with the controller in the function where you present it.

How to segue programmatically segue

I've just finished a Swift tutorial which does not use Interface Builder. It is all programmatic. Everything looks great but now I have to segue back to the storyboard, I am lost.
I'd like to segue from the "Login" button designed in the Audible tutorial to the next view controller I created in my storyboard called "DashboardVC".
Here is a link to the tutorial and source code. https://www.letsbuildthatapp.com/course_video?id=382
TIA
As is all from code, you don't have segue's; either you have to push a new controller using pushViewController from NavigationController or instantiate a new ViewController, in this case your 'DashboardVC'.
Like this
let viewController = DashboardVC()
viewController.view.backgroundColor = .blueColor() //example
navigationController?.pushViewController(viewController, animated: true)
Or just presenting the controller using this
let vc = DashboardVC()
present(vc, animated: true, completion: nil)
Using Storyboard, should be like this; the view controller identifier have to be set in the storyboard as well
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"DashboardVC") as! DashboardVC
self.present(viewController, animated: true)

Presenting a a ViewController on a xib file in SWIFT

I have a xib file which I have created a pop up controller on a UIView. This pop up controller animates up when a button is pressed on a View Controller 1. I then have a button on the UIView which when pressed I want to present another View Controller (View Controller 2). code looks like:
class PopUpViewControllerSwift : UIViewController {
#IBAction func goToVC2(sender: UIButton) {
self.removeAnimate()
let VC2: VC2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as VC2
var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
VC2.modalTransitionStyle = modalStyle
presentViewController(VC2, animated: true, completion: nil)
}
}
Although thou when the button is pressed it crashes, no error or callbacks or anything. Obviously this would normally work if it was just a regular View Controller but because I am doing it inside a pop Up View which has been animated on top of another View I think that is the problem?
Can anybody help?
thanks
I did this in a UICollectionView didSelectItemAtIndexPath method.
// Register Nib
let newViewController = NewViewController(nibName: "NewViewController", bundle: nil)
// Present View "Modally"
self.present(newViewController, animated: true, completion: nil)
I set the ID of the ViewController to the same as the file name so I would be sure to reference the right ViewController.
Make sure self.storyboard is not nil.
Also confirm that VC2 is one of the ViewController's identity in the storyboard file.
I hope it could help you.
if VC2 is in a storyboard file, try to use following command to get VC2
let storyboard = UIStoryboard(name: "YourStoryboardName(maybe 'Main')", bundle: nil)
let vc2 = storyboard.instantiateViewControllerWithIdentifier("VC2") as VC2
or if VC2 is in a nib file, try to use following command to get VC2
let vc2 = VC2(nibName: "YourNibName", bundle: nil)
It should be like this:
let vc = MyViewController(nibName: "MyViewController", bundle: nil)
present(vc, animated: true, completion: nil)