How can I load storyboard programmatically from class? How can I load storyboard programmatically from class? ios ios

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];


Swift 3

let storyboard = UIStoryboard(name: "Main", bundle: nil)let vc = storyboard.instantiateViewController(withIdentifier: "viewController")self.navigationController!.pushViewController(vc, animated: true)

Swift 2

let storyboard = UIStoryboard(name: "Main", bundle: nil)let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")self.navigationController!.pushViewController(vc, animated: true)

Prerequisite

Assign a Storyboard ID to your view controller.

Storyboard ID

IB > Show the Identity inspector > Identity > Storyboard ID

Swift (legacy)

let storyboard = UIStoryboard(name: "Main", bundle: nil)let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewControllerself.navigationController!.pushViewController(vc!, animated: true)

Edit: Swift 2 suggested in a comment by Fred A.

if you want to use without any navigationController you have to use like following :

    let Storyboard  = UIStoryboard(name: "Main", bundle: nil)    let vc = Storyboard.instantiateViewController(withIdentifier: "viewController")    present(vc , animated: true , completion: nil)


In attribute inspector give the identifier for that view controller and the below code works for me

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];[self.navigationController pushViewController:detailViewController animated:YES];