简体   繁体   中英

Presenting Modal View Controller From Xib

I would like to show a modal dialog from a xib. The code that shows my window is:

self.vcSettings = [[ViewControllerSettings alloc] initWithNibName:@"ViewControllerSettings" bundle:[NSBundle mainBundle]];  

[self presentModalViewController:self.vcSettings animated:YES];

When this runs though, I get a blank screen, and not what was inside of my ViewControllerSettings.xib. I imagine I'm showing the view incorrectly somehow. Any advice is appreciated.

EDIT:

I think this should be

[self.navigationController presentModalViewController:self.vcSettings animated:YES];

but for some reason self.navigationController is nil.

EDIT:

Self is a UIViewController instantiated in my AppDelegate like so:

UIViewController* viewMain = [[ViewController_iPhone alloc]     initWithNibName:@"ViewController_iPhone" bundle:nil];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = viewMain;
[self.window makeKeyAndVisible];

You need to create a UINavigationController , set it's root view to the XIB controller that you want to present (in your case vcSettings ) , then present the UINavigationController

self.vcSettings =  [[ViewControllerSettings alloc] initWithNibName:@"ViewControllerSettings" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vcSettings];
[self.navigationController presentModalViewController:controller animated:YES];

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