Ali Ali - 5 months ago 8
Objective-C Question

Navigate from a ViewController to another

I have this design in my storyboard:

enter image description here

As you see the middle ViewController is connected to a Navigation Controller ( I just made the navigation bar invisible). In this middle page I add all the controls programmatically ( using Parse mobile platform login wizard ).

The thing is I want to navigate to the third page, when Login is done successfully.

- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES];
ViewController2 *viewController = [[[ViewController2 alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
printf("%s", [@"Ali" UTF8String]);
}


the message will be printed, but the navigation is failed. Can you help? Perhaps I need to fix something in storyboard.

Answer

I'm not sure you should be sending the dismissModalViewControllerAnimated: message. Are you presenting another view controller modally on top of your login view controller?

Anyway, when you have configured a view controller in a storyboard, you can't create the view controller using alloc and init. You need to ask the storyboard to create it. There are a couple of ways to do this.

One way

One way to get the storyboard to create your ViewController2 is to make a push segue in your storyboard.

  1. Open your storyboard.
  2. Control-drag from the login view controller to the ViewController2.
  3. Choose the “push” segue type.
  4. Click on the segue.
  5. Choose View > Utilities > Show Attributes Inspector.
  6. In the Attributes Inspector (right-hand side of the window), set the segue identifier to “didLogIn”.

To execute the segue, do this:

- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
    [self dismissModalViewControllerAnimated:YES];
    [self performSegueWithIdentifier:@"didLogIn" sender:self];
}

Another way

Another way to get the storyboard to create your ViewController2 is to give it a storyboard ID and ask the storyboard to instantiate the view controller by ID. Then you can push the view controller.

Before you can ask the storyboard to create it, you must give a “storyboard ID” to the ViewController2 instance in your storyboard.

  1. Open your storyboard.
  2. Select the ViewController2 instance.
  3. Choose View > Utilities > Show Identity Inspector.
  4. In the Identity Inspector (right-hand side of the window), enter “viewController2”. Case is important!

Then, in your code, ask the storyboard to instantiate viewController2:

- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
    [self dismissModalViewControllerAnimated:YES]; // Should this be here?
    ViewController2 *viewController = [[[ViewController2 alloc] init] autorelease];
    [self.navigationController pushViewController:viewController animated:YES];
}
Comments