从Xib中呈现模态视图控制器

4

我想从xib文件中展示一个模态窗口。显示窗口的代码如下:

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

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

当我运行这个程序时,我得到的是一个空白屏幕,而不是ViewControllerSettings.xib中的内容。我想我可能以某种方式错误地显示了视图。欢迎任何建议。
编辑:我认为应该是

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

但出于某些原因,self.navigationController 为空。

编辑:

Self 是在我的 AppDelegate 中实例化的 UIViewController,如下所示:

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];

将方法切换为:[ self presentViewController:self.vcSettings animated:YES completion: ^ { NSLog(@"%@", self.presentedViewController); }];控制台上会打印什么?self.presentedViewController对象是否为空? - SushiGrass Jacob
1
请确保视图控制器的 view 插座已连接到其 xib 中的根视图。 - Vadim Yelagin
@Vladim,谢谢,视图已在根视图xib中连接。 - dev
@Jacob,如果我运行它,那一行会在主函数中给我一个“sigabrt”的错误。 - dev
1个回答

10

您需要创建一个UINavigationController,将其根视图设置为您想要展示的 XIB 控制器(在您的情况下为vcSettings),然后呈现UINavigationController

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接