How to navigate to a view controller on a new storyboard file in objective-c
Solution 1
It's right suggestion.
You should get storyboard object by its name. Then you should create your viewController using [storyboard instantiateViewControllerWithIdentifier:@"..."];
Then you may navigate to your new VC using appropriate navigation method. For example:
[self.navigationController pushViewController:vc animated:YES]
ViewController identifier you should set in Interface Builder under section "Identity Inspector" in field "Storyboard ID".
That code you should place in IBAction method. But better would be to extract it in separate method. Something like this:
- (IBAction)buttonPressed:(UIButton *)sender {
[self navigateToMyNewViewController];
}
- (void)navigateToMyNewViewController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myNewStoryboard" bundle:nil];
MyNewViewController *myNewVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyNewViewController"];
[self.navigationController pushViewController:myNewVC animated:YES];
}
Navigation from MyNewViewController your can implement as you want. It may be segues.
Solution 2
MyNewViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:@"YOUR_VC_NAME_HERE"];
This will instantiate your ViewController, from here you can present it like so
[self presentViewController:myVC animated:YES completion:nil]
The image shows where you set the storyboard name that you're referring to
Solution 3
Try This it's working for me ,
Use below line of code :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"bundle:nil];
LoginViewController *loginVC = (LoginViewController*)[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:loginVC animated:YES];
We need to set storyboard id as per mention in below,
T. Israel
Updated on June 17, 2022Comments
-
T. Israel almost 2 years
I got a source code for an existing app. throughout the app the developer didn't use storyboard for the user interface but rather he used interface builder
xib
files. As a new developer, i don't know how to developiOS
apps inxcode
usingxib
files forviewcontrollers
, ilearnt
the storyboard way. Now I want to add more screens to the apps source code, i created a new storyboard file to define my new screens, how do I navigate to the entry point of this new storyboard from a button action i defined in one of thexib
files so that i can easily implement my features. while searching i got some suggestions like this oneUIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"newStoryboard_iPhone" bundle:nil]; MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"myViewCont"];
but i dont think i understand how to implement this even if this can be in a button
IBAction
method. and besides subsequentnavigations
would be segues through the view controllers i define in the new storyboard -
T. Israel over 8 yearsWhat if the new storyboard's initial view is a TabBarController consisting of two viewcontrollers, how do i navigate to the TabBarViewController which will display screen based on first selected tabbar ?index
-
T. Israel over 8 yearsWhat if the first screen is a TabBarViewController, would the code change, because the tab bar controller has two view controllers (assuming two tabs)?
-
Dmitry Arbuzov over 8 yearsYou should instantiate and push(present) TabBarController. If initial selected VC index should be different from 0, you should write tabBarController.selectedIndex = 1; before push or in TabBarController's viewDidLoad
-
Jim Tierney over 8 yearsUse Storyboard ID for the TabBarController, like you would do to call a viewController. This will instantiate the TabBar to display, which is set up to show the two viewControllers from within the TabBar as you say