44

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

Any help is appreciated!

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
mishajw126
  • 517
  • 1
  • 5
  • 14

11 Answers11

87

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];
SlateEntropy
  • 3,988
  • 1
  • 23
  • 31
  • 4
    Just to note that `presentModalViewController:animated:` has been marked as deprecated in the [UIViewController docs](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html). `presentViewController:animated:completion:` should be used instead. –  Jun 07 '12 at 15:58
  • 2
    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 ? – user2533527 Nov 11 '13 at 09:57
  • 7
    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]`. – Rob Oct 29 '14 at 02:09
  • @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? – Avijit Nagare Aug 30 '16 at 11:11
  • Yes, each time it would create new instance. Earlier instances would persist until you dismiss/pop them off the stack. – Rob Aug 30 '16 at 14:27
33

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
sree_achu
  • 331
  • 3
  • 2
  • This pushes continous views as long as I press a row in a DynamicTableView cell, for example: [link]http://stackoverflow.com/questions/19329723/ipad-masterdetail-template – LAOMUSIC ARTS Oct 15 '13 at 17:30
  • ok, if the story board is called Main.Storyboard, you need to put just @"Main", instead of @"Main.storyboard" – tong Sep 19 '14 at 13:47
18

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)
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
17

The instantiateViewControllerWithIdentifier is the Storyboard ID.

NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];
Lukas Wiklund
  • 826
  • 6
  • 21
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];
aturan23
  • 4,798
  • 4
  • 28
  • 52
BlueAMG63
  • 51
  • 1
  • 2
4

Swift 3.0 Version

if you want to present new controller.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "controllerIdentifier") as! YourController
self.present(viewController, animated: true, completion: nil)

and if you want to push to another controller (if it is in navigation)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "controllerIdentifier") as! YourController
self.navigationController?.pushViewController(viewController, animated: true)
Community
  • 1
  • 1
Sahil
  • 9,096
  • 3
  • 25
  • 29
  • Identifier = Storyboard ID. To set the Identifier, select the view controller in the storyboard > Identity inspector > Storyboard ID under Identity. – Mark Gerrior Dec 07 '18 at 02:49
3
#import "YourViewController.h"

To push a view including the navigation bar and/or tab bar:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
YourViewController *viewController = (YourViewcontroller *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
[self.navigationController pushViewController:viewController animated:YES];

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

Nico Nimz
  • 268
  • 2
  • 15
2

This worked for me:

NSTimer *switchTo = [NSTimer scheduledTimerWithTimeInterval:0.1
           target:selfselector:@selector(switchToTimer)userInfo:nil repeats:NO];

- (void) switchToTimer {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyViewControllerID"]; // Storyboard ID
[self presentViewController:vc animated:FALSE completion:nil];
}
Walter Schurter
  • 997
  • 9
  • 14
1
[self.navigationController pushViewController:someViewController animated:YES];
StuR
  • 12,042
  • 9
  • 45
  • 66
  • 1
    This does not work for me, does it matter that im doing it in the tableView:didSelectRowAtIndexPath: method? – mishajw126 Jun 07 '12 at 14:59
1

Swift 3.0 in

one view controller to secondviewcontroller go:

let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MsgViewController") as! MsgViewController
        self.navigationController?.pushViewController(loginVC, animated: true)

2nd viewcontroller to 1stviewcontroller(back)for: back button on action event:-

self.navigationController?.popViewController(animated:true)

3rdviewcontroller to 1st viewcontroller jump for

self.navigationController?.popToRootViewController(animated:true)

and most important storyboard in navigation bar unclicked make sure than this back action perform

ronak patel
  • 400
  • 5
  • 17
0

I think the OP is asking how to swap a VIEW without changing viewCONTROLLERs. Am I misunderstanding his question?

In pseudocode, he wants to do:

let myController = instantiate(someParentController)

let view1 = Bundle.main.loadNib(....) as... blah
myController.setThisViewTo( view1 )

let view2 = Bundle.main.loadNib(....) as... blah
myController.setThisViewTo( view2 )

Am I getting his question wrong?

Deepzz
  • 4,573
  • 1
  • 28
  • 52
ChrisH
  • 975
  • 12
  • 21