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];
Related videos on Youtube
Author by
mishajw126
Updated on February 12, 2021Comments
-
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 about 12 yearsThis does not work for me, does it matter that im doing it in the tableView:didSelectRowAtIndexPath: method?
-
Admin about 12 yearsJust to note that
presentModalViewController:animated:
has been marked as deprecated in the UIViewController docs.presentViewController:animated:completion:
should be used instead. -
LAOMUSIC ARTS over 10 yearsThis 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 over 10 yearsI 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 almost 10 yearsok, if the story board is called Main.Storyboard, you need to put just @"Main", instead of @"Main.storyboard"
-
Rob over 9 yearsNeedless to say, you now rarely instantiate view controllers by calling
alloc
andinit
. 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 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 almost 8 yearsYes, each time it would create new instance. Earlier instances would persist until you dismiss/pop them off the stack.
-
Mark Gerrior over 5 yearsIdentifier = Storyboard ID. To set the Identifier, select the view controller in the storyboard > Identity inspector > Storyboard ID under Identity.
-
Mark Gerrior over 5 yearsLast sentence does not apply to Xcode 10 due to UX changes.