How can I switch views programmatically in a view controller? (Xcode, iPhone)

115,505

Solution 1

If you're in a Navigation Controller:

ViewController *viewController = [[ViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];

or if you just want to present a new view:

ViewController *viewController = [[ViewController alloc] init];    
[self presentViewController:viewController animated:YES completion:nil];

Solution 2

If you want to present a new view in the same storyboard,

In CurrentViewController.m,

#import "YourViewController.h"

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourViewController *viewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
[self presentViewController:viewController animated:YES completion:nil];

To set identifier to a view controller, Open MainStoryBoard.storyboard. Select YourViewController View-> Utilities -> ShowIdentityInspector. There you can specify the identifier.

Solution 3

Swift version:

If you are in a Navigation Controller:

let viewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("VC") as ViewController
self.navigationController?.pushViewController(viewController, animated: true)

Or if you just want to present a new view:

let viewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("VC") as ViewController
self.presentViewController(viewController, animated: true, completion: nil)

Solution 4

The instantiateViewControllerWithIdentifier is the Storyboard ID.

NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];

Solution 5

To dismiss the Viewcontroller called with the previous answers code by CmdSft

ViewController *viewController = [[ViewController alloc] init];    
[self presentViewController:viewController animated:YES completion:nil];

you can use

[self dismissViewControllerAnimated:YES completion: nil];
Share:
115,505

Related videos on Youtube

mishajw126
Author by

mishajw126

Updated on February 12, 2021

Comments

  • mishajw126
    mishajw126 over 3 years

    Have been struggling with this for a while, and can never seem to get a direct answer.

    Any help is appreciated!

  • mishajw126
    mishajw126 about 12 years
    This does not work for me, does it matter that im doing it in the tableView:didSelectRowAtIndexPath: method?
  • Admin
    Admin about 12 years
    Just to note that presentModalViewController:animated: has been marked as deprecated in the UIViewController docs. presentViewController:animated:completion: should be used instead.
  • LAOMUSIC ARTS
    LAOMUSIC ARTS over 10 years
    This pushes continous views as long as I press a row in a DynamicTableView cell, for example: [link]stackoverflow.com/questions/19329723/ipad-masterdetail‌​-template
  • user2533527
    user2533527 over 10 years
    I am just at this question , if I use the second solution will I be able to go back to the previous view , and how ?
  • tong
    tong almost 10 years
    ok, if the story board is called Main.Storyboard, you need to put just @"Main", instead of @"Main.storyboard"
  • Rob
    Rob over 9 years
    Needless to say, you now rarely instantiate view controllers by calling alloc and init. With storyboards, you use [self.storyboard instantiateViewControllerWithIdentifier@:"storyboard id"]. Or if using storyboards with segue, you'd just perform segue directly with [self performSegueWithIdentifier:@"storyboard id" sender:self];, which will both instantiate new view controller and transition to it in one fell swoop. And if using NIBs, if the NIB name is different, you'd instantiate view controller with [[NSBundle mainBundle] loadNibNamed:@"nibname" owner:self options:nil].
  • Avijit Nagare
    Avijit Nagare almost 8 years
    @Rob. The line code [self.storyboard instantiateViewControllerWithIdentifier@:"storyboard id"]; execute multiple times then new *vc object created every time? what about earlier *vc object ? is this memory leak?
  • Rob
    Rob almost 8 years
    Yes, each time it would create new instance. Earlier instances would persist until you dismiss/pop them off the stack.
  • Mark Gerrior
    Mark Gerrior over 5 years
    Identifier = Storyboard ID. To set the Identifier, select the view controller in the storyboard > Identity inspector > Storyboard ID under Identity.
  • Mark Gerrior
    Mark Gerrior over 5 years
    Last sentence does not apply to Xcode 10 due to UX changes.