How to Load Storyboard Programmatically from Class

How can I load storyboard programmatically from class?

In your storyboard go to the Attributes inspector and set the view controller's Identifier. You can then present that view controller using the following code.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

Programmatically load ViewController from Storyboard in app with multiple storyboards

The solution (as mentioned in the comments above) is to not include the .storyboard when providing the name of the storyboard.

Old

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Home.storyboard" bundle:nil];

New

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Home" bundle:nil];

How to load storyboard viewcontroller into programmatically tabbar controller in ios siwft?

Please try below code. you can give view controller identifier by interface builder.

let tabBarCnt = UITabBarController()
func createTabBarController() {
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let firstVC = storyboard?.instantiateViewControllerWithIdentifier("FViewController")
firstVC.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 0)

let secondVC = storyboard?.instantiateViewControllerWithIdentifier("SViewController")
secondVC.tabBarItem = UITabBarItem(tabBarSystemItem: .more, tag: 1)

let thirdVC = storyboard?.instantiateViewControllerWithIdentifier("ViewController")
thirdVC.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 2)

let fourthVC = storyboard?.instantiateViewControllerWithIdentifier("ViewController")
fourthVC.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 3)

var controllerArray = [firstVC, secondVC]
if UIDevice.current.userInterfaceIdiom == .pad {
controllerArray += [thirdVC, fourthVC]
}
tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: $0)}
self.view.addSubview(tabBarCnt.view)
}

How to load UIViewController programmatically from storyboard?

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.

Sample Image

Present View From Storyboard Programmatically

Your screenshot indicates you have not created a segue between the two view controllers. Do so by click-and-drag from one view controller to the other. In the attribute inspector, give your newly-created segue an identifier, i.e., a string that names it.

To trigger this segue programmatically, in your source view controller (the one you are segueing from) just call performSegueWithIdentifier and pass in the identifier you chose.

Instantiate and Present a viewController in Swift

This answer was last revised for Swift 5.4 and iOS 14.5 SDK.


It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.

let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
self.present(vc, animated: true)

Make sure to set myVCID inside the storyboard, under "Storyboard ID."

Sample Image



Related Topics



Leave a reply



Submit