简体   繁体   中英

Changing view controllers without the storyboard

I am trying to change views but I want have to do it through code and not through storyboard for reasons that don't really matter, and I found this code:

[[self ] presentModalViewController:[self destinationViewController] animated:NO];

the problem is I don't know what to put in for presentModalViewController or destinationViewController. Is there some way to figure out what my views are called so i can put them in?

At the top you should have

#import "NameOfViewController.h"

Then later

UIViewController *destinationViewController = [[NameOfViewController alloc] init];
[self presentViewController:destinationViewController animated:NO completion:nil];

Use presentViewController instead of presentModalViewController as the latter method is deprecated. I hope that works for you.

In the example you give, [self destinationViewController] creates and returns a UIVIewController.

You can either:

  1. load one from a NIB file

    controller = [[MyController alloc] initWithNibName:@"nib-resource-name" bundle:nil];

  2. load one from the storyboard

    MyController * controller = [self.storyboard instantiateViewControllerWithIdentifier:@"controller-storyboard-id"];

  3. create one programmatically

    How do I create a UIViewController programmatically?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM