Present storyboard ViewController from another ViewController

32,257

Solution 1

Assuming you have storyboard, go to storyboard and give your VC an identifier (inspector), then do:

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

Assuming you have a xib file you want to do:

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"NIBNAME" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

Without a xib file:

UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];

Solution 2

Following will work on Swift 3.0 and above.

StoryBoard

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "Identifier")
self.navigationController?.pushViewController(mainViewController, animated: true)

.xib

    let viewController = UIViewController(nibName: "NibName", bundle: nil)
    self.navigationController?.pushViewController(viewController, animated: true)

Without .xib

let viewController = UIViewController()
self.navigationController?.pushViewController(viewController, animated: true)

Solution 3

Update with the Swift 3.1 version

if you haven't embed navigation controller then

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.present(viewController2, animated: true, completion: nil)

and, if you had embed navigation controller then

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.navigationController?.pushViewController(viewController2, animated: true)

Solution 4

Swift 5

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

Solution 5

In swift 4.2 answer for those who want this answer without navigation in modally and in swift updated versions.

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "mainViewController")
self.present(controller, animated: true, completion: nil)
Share:
32,257
Ranjit
Author by

Ranjit

Passion to code for iPhone :)

Updated on July 29, 2022

Comments

  • Ranjit
    Ranjit almost 2 years

    I have multiple UIViewControllers in my iOS App's Storyboard. I want to open one of the UIViewControllers (in the storyboard) from a different UIViewController.

    I've tried the code below, but it isn't working even though I used it before iOS 5 and it worked fine.

    - (IBAction)addNotes:(id)sender {
        NotesViewController *notesView = [[NotesViewController alloc] initWithNibName:@"NotesViewController" bundle:nil];
        [self presentModalViewController:notesView animated:YES];
    }
    

    How can I perform this action with iOS 5 Storyboards?