Jackson Wang
Jackson Wang

Reputation: 61

How conditionally change to a new storyboard

i'm writing a little IOS app using Xcode 7 IOS 9 (objective-c). Right now I'm trying to switch from the main storyboard to the second storyboard by pressing a button called "confirm". How can i write code in ViewController.m to conditionally go to the second storyboard? So if all input is correct, user presses confirm it will go to the new storyboard, else stay and display warning message. Thank you!

Upvotes: 0

Views: 65

Answers (2)

Hardik Shekhat
Hardik Shekhat

Reputation: 1878

Try this code. May be help you.

//button Actions
- (IBAction)loginbtnClicked:(id)sender
{
    [self.tfEmail resignFirstResponder];
    [self.tfPasswrd resignFirstResponder];
    if ([self.tfEmail.text isEqualToString:@""] || [self.tfPasswrd.text isEqualToString:@""])
    {
          UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@“Title” message:@"You are required to fill out all fields." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
          [alert show];
          return;
    }
   if ([self isEmailValid:self.tfEmail.text])
   {
         //put your required code here.

        //Navigate to next view controller.
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main” bundle:nil];
        HomeVC *homeVC =[storyboard instantiateViewControllerWithIdentifier:@"HomeVCID"];
        [self.navigationController pushViewController:chatVC animated:TRUE];
   }
   else
   {
         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@“Title” message:@"Please enter valid email address." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
   }
}

Upvotes: 0

Nilesh Patel
Nilesh Patel

Reputation: 6394

    UIStoryboard *storyBoard;
    if (yourcondition)
           storyBoard = [UIStoryboard storyboardWithName:@"StoryBoard1" bundle:[NSBundle mainBundle]];
    else
            storyBoard = [UIStoryboard storyboardWithName:@"StoryBoard2" bundle:[NSBundle mainBundle]];

    YourTargetViewController *targetCon = [stryBoard instantiateViewControllerWithIdentifier:@"TargetControllerID"];
    [self.navigationController pushViewController:targetCon animated:YES];

Hope it helps..

Upvotes: 1

Related Questions