How to load UIViewController programmatically from storyboard? - ios

I created a UIViewController in my Main.storyboard with a few buttons and labels. I'm trying to switch to that view controller using self.presentViewController but it will not load the view from storyboard. It will only load a blank black screen by default. Any idea on how to load the view from what i've created in storyboard?
self.presentViewController(ResultViewController(), animated: true, completion: nil)

The way you're doing this just creates a new instance of your view controller. It does not create one from the prototype you've defined in Interface Builder. Instead, you should be using this, where "SomeID" is a storyboard ID that you've assigned to your view controller in Interface Builder.
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SomeID") as? ResultViewController {
presentViewController(resultController, animated: true, completion: nil)
}
You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.

Full Swift 3 code including instantiation of Storyboard:
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
if let mainViewController = storyboard.instantiateInitialViewController() {
present(mainViewController, animated: false, completion: nil)
}

SWIFT 5.2
first, you should define a storyboardID for viewcontroller and after use this code
let storyboard = UIStoryboard(name: "Main", bundle: nil) // type storyboard name instead of Main
if let myViewController = storyboard.instantiateViewController(withIdentifier: "myViewControllerID") as? CourseDetailVC {
present(myViewController, animated: true, completion: nil)
}

Related

Display navigation when a viewController is displayed programatically

I'm using https://github.com/jonkykong/SideMenu for my Swift 4 application. It's working good, but I have problems to display a new ViewController programmatically and the NavigationController.
If I use:
let vc = MyViewController.self() //your view controller
self.present(vc, animated: true, completion: nil)
All the IBOutlet are nil and the ViewController is not loaded.
And the same happens with:
let vc = MyViewController()
self.navigationController?.pushViewController(vc, animated: true)
How I can display a new ViewController keeping the Navigator of the SideMenu?
If you are not creating the VC's view programmatically in code , you have to load it either from storyboard
let yourVC = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
or nib
let yourVC = YourVC(nibName: "YourVCNibName", bundle: nil)

Display multiple UIViewControllers in Storyboard by implementing codes

More specific,
Let's say I have a UIViewController A, when I press a button in A that is created by code, I would like to display another UIViewController B, where B is designed in the storyboard. Can it be done by coding, but not using the connections between view controllers in the storyboard. Thanks.
Absolutely,
Make sure you have added the storyBoardIdentifier to ViewController B.
Finally, In your ViewController A :
let YourStoryBoard = UIStoryboard(name: "YourStoryBoard", bundle: nil)
let vc = YourStoryBoard.instantiateViewController(withIdentifier: "VCB_StoryBoard_Identifier") as! ViewControllerB
If your ViewController has a UINavigationController embedded to it then,
self.navigationController?.pushViewController(vc, animated: true)
Else present ViewController B from ViewController A
self.present(vc, animated: true, completion: nil)
EDIT:
Set your storyboard identifier as shown in image below
Associate your ViewControllerB class with storyboard VC by setting the ViewController's class. As shown below.
You have to make a new cocoa touch class -> UIViewController -> "ViewContollerB"
Add the name in the storyboard to the view and than you can do:
let storyboard = UIStoryboard(name: "YourStoryBoard", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "VCB_StoryBoard_Identifier") as! ViewContollerB
Try this:
On button tap in UIViewController A,
func onTapButton()
{
let controller = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerBIdentifier") as! ViewControllerB
self.present(controller, animated: true, completion: nil)
}
Here,
ViewControllerBIdentifier : StoryboardID corresponding to UIViewController B
ViewControllerB : class corresponding to UIViewController B

Create a new view with swift

I am new with iOS an in this website. I was using a navigation controller and pushViewController to switch to other views. But now I am trying to push to a view without a navigation bar. The new view should contain a Done button which once clicked should bring the user back to the last view. Do you have an idea how to achieve that? I tried this
let storyboard = UIStoryboard(name: "Main.Storyboard", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "FormViewController")
self.present(controller, animated: true, completion: nil)
I got an error
'Could not find a storyboard named 'Main.storyboard' in bundle NSBundle. I get the same error if I try FormViewController as the story board name. I set FormViewController as Storyboard ID and also as the swift file name I want to use with the interface, and Main.Storyboard is the name of the storyboard file.
You don't add the ".storyboard" part of the filename, it does that automatically:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
When dealing with the storyboard and accessing ViewController, you will have to follow these steps:
1. Assign Storyboard ID to ViewController
In your Storyboard, select the desired ViewController and assign Storyboard Id to it
2. Access the ViewController
To access the ViewController, you will need the Storyboard Id which we have set in the first step.
If you have only one storyboard you can simply do like this
let controller = self.storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
self.present(controller, animated: true, completion: nil)
**
OR
**
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
self.present(controller, animated: true, completion: nil)
If you are using multiple storyboards within the same project then you will have to access storyboard with it's name (without its extension i.e .Storyboard):
let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
self.present(controller, animated: true, completion: nil)

How do I connect programmed ViewControllers with new views created in storyboard?

I am working with a team and our first two views in our workflow are created programmatically. We decided afterwards that we should use storyboards to facilitate some of the UI design. The initial view controller is LoginController, then it connects to UserProfileViewController. I want to design the UI for the second view in storyboards and have it connect to UserProfileViewController. How would I do this? It works if I make the view in the storyboard the initial view. If not it does not work.
If you want to push to a Storyboard programatically here is how you do it in Swift.
//Swift 3.0
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.navigationController?.pushViewController(controller, animated: true)
The link below is another post on how to present a modal ViewController
//Swift 3.0
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil)
Instantiate and Present a viewController in Swift

UICollectionView is nil

I have a UIViewController in storyboard which hosts a collectionView which is referenced using a IBOutlet. However, now I am placing this inside of a PageViewController and referencing it like this, rather than with the use of a seque :
let initial = FeedCollectionViewController()
let viewControllers = [initial]
setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
The problem is that the collectionView is now nil.
Of course it's nil. You built your view controller in storyboard, so you should init it from storyboard. Like this:
let tabSb = UIStoryboard(name: "Main", bundle: nil)
let tabbarVc = tabSb.instantiateInitialViewController()

Resources