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

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


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


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 YourViewControllerView-> Utilities -> ShowIdentityInspector.There you can specify the identifier.


Swift version:

If you are in a Navigation Controller:

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

Or if you just want to present a new view:

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