Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate to a view controller on a new storyboard file in objective-c

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 develop iOS apps in xcode using xib files for viewcontrollers, i learnt 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 the xib files so that i can easily implement my features. while searching i got some suggestions like this one

UIStoryboard *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 subsequent navigations would be segues through the view controllers i define in the new storyboard

like image 956
T. Israel Avatar asked Jan 18 '16 09:01

T. Israel


1 Answers

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.

like image 151
Dmitry Arbuzov Avatar answered Sep 21 '22 17:09

Dmitry Arbuzov