如何以模态方式呈现标准 UIViewController
我试图以模态方式呈现一个标准的 ViewController ,但不知道如何做到这一点。视图控制器将具有最终触发解除操作的按钮,因此我不需要将其包装在 NavigationController
中。另外,我以编程方式完成所有这些操作,没有 .xibs。
这是我正在使用的代码:
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"view did appear within rootviewcontroller");
WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
[self presentModalViewController:welcome animated:true];
[welcome release];
}
问题是我还没有设置 WelcomeViewController's
视图,因此 loadView 没有运行,这意味着没有内容被绘制到屏幕上。
我发现的每个示例(包括 Apple 的示例)都使用 .xib 来初始化 ViewController、添加 RootViewController 的 NavigationController,或两者都使用。我的理解是,在这两种情况下, loadView 都会自动为您调用。 http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3
在哪里配置我的 WelcomeViewController 的
视图?就在 alloc/init 之后吗?在 WelcomeViewController 的
init 方法中?
谢谢!
I'm trying to present a standard ViewController
modally, but can't figure out how to do it. The view controller will have buttons which will ultimately trigger the dismissing actions, so I don't need to wrap it in a NavigationController
. Also, I'm doing all of this programmatically, without .xibs.
Here's the code I'm using:
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"view did appear within rootviewcontroller");
WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
[self presentModalViewController:welcome animated:true];
[welcome release];
}
The problem is that I haven't set the WelcomeViewController's
view, so loadView is not getting run, which means no content is being drawn to the screen.
Every example I find, including Apple's, uses either a .xib to initialize the ViewController, a NavigationController that adds a RootViewController, or both. My understanding is that loadView is called automatically for you in both of these scenarios.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3
Where do I configure my WelcomeViewController's
view? Right there after the alloc/init? Inside WelcomeViewController's
init method?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重写子类中的
loadView
方法。请参阅 查看 iOS 控制器编程指南。Override the
loadView
method in your subclass. See View Controller Programming Guide for iOS.下面是一个简单的示例,说明如何在不使用 NIB 的情况下完成此操作:
在 AppDelegate
didFinishLaunchingWithOptions:
中,创建自定义视图控制器的实例并将其添加为窗口的子视图(非常标准)。创建 vc 实例时,您使用指定的初始化程序,该初始化程序将在视图控制器的新实例上调用。您不指定任何笔尖,因为您将在方法内进行自定义初始化:
Here's a simple example of how you can go about it without using NIBs:
In your AppDelegate
didFinishLaunchingWithOptions:
, you create an instance of your custom view controller and add it as a subview of window (pretty standard).When creating the
vc
instance, you use the designated initialiser which will be called on the new instance of the view controller. You don't specify any nibs because you will do your custom initialisation inside the method: