rock_n_rolla
rock_n_rolla

Reputation: 357

Moving to a second view controller from one view controller programatically

  1. I have added a second View Controller to my Main.storyboard. I have given it a title lbvc. My first view controller is named JPViewController
  2. In my first view controller JPViewController.m file, I'd like to move to that view controller, in code, inside a specific method.

The code I have right now, but I think there are some problems, specifcally on the second line where I am not sure what to have in place of SomethingHere.

Thanks very much in advance guys!

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SomethingHere *viewController = [storyboard instantiateViewControllerWithIdentifier:@"lbvc"];
[self presentViewController:viewController animated:YES completion:nil];

Upvotes: 1

Views: 93

Answers (4)

Amit Singh
Amit Singh

Reputation: 2698

First answer to you question, if you are instantiatingViewController by its storyboard id, it means that you are sure what type of object it will return. The instantiateViewController method always return UIViewController type. So you can cast it to your ViewController type.

SecondVC *secondVC = (SecondVC *)[storyboard instantiateViewControllerWithIdentifier:@"lbvc"];

Secondly, Best way to use the storyboard object is to get the object from which the ViewController was loaded. Source

//If creating it from AppDelegate Class
UIViewController *viewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"lbvc"];
//If you are creating it in some ViewController
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"lbvc"];

enter image description here

The reason of using it as suggested is because if you will try to get object using the code below, it will create a new object of storyboard and then return. Source

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

enter image description here

Upvotes: 0

Pankaj Rathor
Pankaj Rathor

Reputation: 375

In case you are using segues to navigate between view controller, use performSegueWithIdentifier:sender: on your view controller object to programmatically invoke the navigation to second view controller:

Upvotes: 0

Jerin T George
Jerin T George

Reputation: 64

you have to give the name of the second view controller in the place of something here, your creating a instance of the secondview controller there and pushing your control from first view to the second in the next line. hop it will help you.

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Replace SomethingHere with UIViewController and put storyboardID for your second vc as lbvc else your app will crash.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"lbvc"];
[self presentViewController:viewController animated:YES completion:nil];

Whenever you are unsure what to use at SomethingHere always use UIViewController, because UIViewController is parent of all ViewControllers.

In some scenarios you want to pass some values to second vc, at that time you will need to use your actual class name which is assigned for second vc.

Example

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondVC *viewController = (SecondVC *)[storyboard instantiateViewControllerWithIdentifier:@"lbvc"];
viewController.strABC = @"abc value";
[self presentViewController:viewController animated:YES completion:nil];

Upvotes: 1

Related Questions