Login Screen using Storyboard

2020-08-01 08:55发布

问题:

I am new to iOS 5 and trying to write my apps as pure iOS 5 app using the new storyboard feature.

I have a start screen (Login Screen) so I do not want to use navigationcontroller because i don't need any user to go back for a log-in screen after log-in successfully moreover, i don't need navigationbar because it's affect on the Home Screen Design.

So, I need to do a manual segue to the login screen, after checking the username and password for login:

If(Login Successful)
     Navigate to the Home View Controller
else
     Display Error Message.

Now is this even possible, or do I need 2 story boards for that ?

回答1:

If(Login Successful){
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

      HomeViewController* viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];

      [self presentViewController:viewController animated:YES completion:nil];
}

Possible Duplicate of Question



回答2:

Since using Navigation controller after embedding navigation controller in scene

If(Login Successful){
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
      HomeViewController* viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
    [self.navigationController pushViewController:HomeViewController animated:YES];
}


回答3:

You can achieve this with a manual segue. In the storyboard control-drag from the login screen view controller to the home view controller (make sure you are zoomed out in the storyboard, so that you are actually selecting the viewcontrollers and not the subviews in them), and select "modal" as the type of segue. Then select the segue itself and in the Attributes inspector give it an identifier, such as "loginSuccessful". Then in you can write:

If(Login Successful)
    [self performSegueWithIdentifier:@"loginSuccessful" sender:self];

This will present the view controller pointed by the segue in the storyboard. If you need to do extra setup of the home screen before showing it, you can do it in the prepareForSegue method like follows:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if(segue.identifier isEqualToString:@"loginSuccessful"){
        HomeViewController* target =(HomeViewController *) segue.destinationViewController;
        Target Setup;
    }
}