Go to a specific UIViewController - swift

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)

Related

Present Next ViewController to Dismiss current ViewController Using Swift

My Scenario, I have three ViewControllers, those ViewControllers First, Second, Third. Here, First ViewController UIButton click to present model second ViewController and then Second ViewController UIButton click to present model Third ViewController. I need to Implement the Second ViewController button click to dismiss current ViewController after presenting Third ViewController because once I close Third ViewController I need to show First ViewController. How to Achieve this?
NOTE: I am having NavigationController for Third ViewController also I am using Storyboard.
Here's my code:
#IBAction func ClickThirdVC(_ sender: Any) {
if let VC_Third = self.storyboard?.instantiateViewController(withIdentifier: "thirdviewcontroller") as? ThirdvViewController {
weak var pvc = self.presentingViewController
VC_B.modalTransitionStyle = .crossDissolve
let navController = UINavigationController(rootViewController: VC_B)
self.dismiss(animated: true, completion: {
pvc?.present(navController, animated: true, completion: nil)
})
}
}
You can try to dismiss self after presenting the third UIViewController, in the completion handler of the present function .
secondVC?.present(navController, animated: true) {
self.dismiss(animated: false, completion: nil) //dismiss self after presenting the third.
}

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)

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)

How can I present a UITabBarController in 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.