尝试从故事板初始化视图控制器 [英] Trying to initialize a view controller from storyboard

查看:107
本文介绍了尝试从故事板初始化视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,我放在主故事板中但是当我尝试初始化并加载视图控制器时,我的应用程序崩溃了。我正在使用的xcode版本并没有真正告诉我我得到的错误,但我确实看到它给了我一个sigabrt信号。我不知道为什么它不起作用。

I have a view controller I placed in the main storyboard but when I try to initialize and load up the view controller my app crashes. The xcode version I'm using doesn't really tell me the errors I get properly, but I did see that it was giving me a sigabrt signal. I don't know why it isn't working.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SelectDateViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];

    [self presentModalViewController:vc animated:YES];

这就是我所说的。我使用的所有名称都是正确的,但我仍然不明白为什么它会崩溃。

This is how I called it. All the names I used are correct but I still don't understand why it's crashing.

推荐答案

在最后一行设置一个断点验证vc(以及故事板)不是零。

Set a breakpoint at the last line to verify vc (and also storyboard) are not nil.

如果其viewController参数为nil,则presentModalViewController将崩溃。

presentModalViewController will crash if its viewController argument is nil.

如果两者都是nil,那么你的故事板名称可能不正确(不太可能)。

If both are nil, then your storyboard name is likely incorrect (less likely).

如果只是vc是nil那么标识符是不正确的(无论是代码还是故事板)。确保VC的Storyboard ID(如屏幕截图中的圆圈所示)与您的描述相符。习惯上不为此选择类名(因为您的故事板中可能有多个给定类的实例)。

If just vc is nil then the identifier is incorrect (either in code or the storyboard). Make sure the VC's Storyboard ID (as circled in the screenshot) matches your description. It is customary not to choose a class name for this (as you might have more than one instance of a given class in your storyboards).

故事板ID区分大小写并且必须是独特的。我发现设置它的确定方法是在选择视图控制器并按返回结束编辑后键入该字段中的名称(不要只是单击该字段)。我注意到在从Xcode 4转换为5时会在故事板中发生一些损坏,反之亦然,所以潜入plist / xml也可能有所帮助。

The Storyboard ID is case sensitive and must be unique. I find the sure fire way to set it is type the name in that field after selecting the view controller and pressing return to end editing (don't just click off the field). I've noticed some corruption occurring in storyboards when converting from Xcode 4 to 5 and vice versa, so diving into the plist/xml might also help.

编辑:
也是时间的时间这被称为很重要,请参阅
presentModalViewController无法正常工作
确保已经显示接收消息的视图控制器(即在viewDidAppear中调用,而不是在viewDidLoad中调用)。

Also the timing of when this is called is important, see presentModalViewController not working. Make sure the view controller receiving the message has already been shown (i.e. called in viewDidAppear, not viewDidLoad).

正如阿卜杜拉所说,你应该移动关闭已弃用的方法:

As mentioned by Abdullah, you should likely move off the deprecated method to:

[self presentViewController:vc animated:YES completion:nil];

在这种情况下,完成块可以是nil而不会出现问题,但vc一定不能。

In this case the completion block can be nil without issue, but vc must not be.

这篇关于尝试从故事板初始化视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆