0

I am building an App that uses three UIViewControllers. I am using navigation controller to navigate. First view controller (v1) will have two text boxes and a button. On the button click, I have a function that checks certain criteria and if its satisfied then I move to the second view controller (v2) or else I want to move to the third view controller (v3). How is it possible through code?

One last thing, I want to capture these text box values and use them in the appropriate view controller. How is it possible?

Gravity M
  • 1,485
  • 5
  • 16
  • 28

2 Answers2

1

In terms of programmatically transitioning to another scene in your storyboard, the general procedure is:

  • define a segue between the view controllers (not the button, but the view controllers);

  • give that segue a storyboard identifier;

  • create an IBAction for a button that does the validation, and if successful, does a

    [self performSegueWithIdentifier:@"myidentifier"];
    

See this answer on stack overflow in which I walked an individual through this process in some detail. If your case, it sounds like you're validating data, and might perform one segue given one series of conditions, and another segue in another series of conditions. So just give those two segues unique identifiers, and then you can perform whichever one your want in your IBAction code.

In terms of passing data to that next scene, you do this in a separate method, prepareForSegue, which iOS calls after the destination controller has been created, but before it's been presented. It's here that you have a chance to pass parameters to the destination controller. For example, I'm assuming that I captured the first and last name of an individual and want to pass that to the destination controller, which has first and last name properties:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"myidentifier"])
    {
        MyDestinationController *controller = segue.destinationViewController;
        controller.firstName = self.labelFirstName.text;
        controller.lastName = self.labelLastName.text;
    }
}
Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

As of iOS 6.0, you can implement -shouldPerformSegueWithIdentifier:sender: in your controller. Much simpler than what you had to do in iOS 5.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129