Present storyboard ViewController from another ViewController Present storyboard ViewController from another ViewController ios ios

Present storyboard ViewController from another ViewController


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


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)


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)


matomo