Change view in one XIB

2019-09-04 03:08发布

问题:

I'm trying to make an app using Interface Builder (instead of Storyboard) for the first time, but I'm stuck. Is it possible to change view using a ViewController in the same XIB? I'd like that when I tap a button, the view changes to the other ViewController present in the same XIB file. Must I call back an action? If yes, which?

回答1:

If you have 2 view controllers with 2 .xib files, like this

You can present the NextViewControler from your ViewController's button press method,

-(IBAction) yourButtonPressed:(id)sender;{
    NextViewController *nextViewController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
    [self presentViewController:nextViewController animated:YES completion:nil];
}

EDIT :

You can present the same viewcontroller like this.

-(IBAction) yourButtonPressed:(id)sender;{
    ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    [self presentViewController:viewController animated:YES completion:nil];
}