Storyboard and Switch Views?

2019-02-11 10:40发布

问题:

I have an iPad app that uses the storyboard board feature and then I have a separate .xib file for another view. I can switch to the separate view and its fine:

-(IBAction)SecondView:(id)sender{

    SecondView *Second = [[SecondView alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:Second animated:NO];

}

But when I am in the Second View and try going back I do everything the same just with the first view controller, but It just goes to a black screen:

-(IBAction)FirstView:(id)sender{

    FirstView *First = [[FirstView alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:First animated:NO];

}

What do you guys think? Am I doing something wrong? What is the best way to switch views?

回答1:

initWithNibName:bundle is for loading nib or xib files.

If you are loading from a storyboard, you need to use

    FirstView *First= [self.storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"];

IDENTIFIER is defined in your storyboard, it's in the Utilities, Attributes Inspector on the right side.

HOWEVER your real problem is that you shouldn't be loading from the storyboard at all. you should just be calling

[self dismissModalViewControllerAnimated:YES];

That call will clean up the presentModalViewController:animated: that you used to put the modal view controller up in the first place.



回答2:

You presented SecondView using presentModalViewController:animated:, so you need to dismiss it using dismissModalViewControllerAnimated:.

- (IBAction)FirstView:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}