Have been struggling with this for a while, and can never seem to get a direct answer.
Any help is appreciated!
Have been struggling with this for a while, and can never seem to get a direct answer.
Any help is appreciated!
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 YourViewController View-> Utilities -> ShowIdentityInspector. There you can specify the identifier.
The instantiateViewControllerWithIdentifier
is the Storyboard ID
.
NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];
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)
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];
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)
#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.
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];
}
[self.navigationController pushViewController:someViewController animated:YES];
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
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?