Storyboard - 参考 AppDelegate 中的 ViewController [英] Storyboard - refer to ViewController in AppDelegate

查看:23
本文介绍了Storyboard - 参考 AppDelegate 中的 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下场景:我有一个基于故事板的应用.我将一个 ViewController 对象添加到故事板,将这个 ViewController 的类文件添加到项目中,并在 IB 身份检查器中指定新类的名称.现在我将如何从 AppDelegate 以编程方式引用这个 ViewController?我已经使用相关类创建了一个变量并将其转换为 IBOutlet 属性,但我没有看到任何能够在代码中引用新 ViewController 的方法 - 任何按住 ctrl 并拖动连接的尝试都不起作用.

consider the following scenario: I have a storyboard-based app. I add a ViewController object to the storyboard, add the class files for this ViewController into the project and specify the name of the new class in the IB identity inspector. Now how am I going to refer to this ViewController programmatically from the AppDelegate? I've made a variable with the relevant class and turned it into an IBOutlet property, but I don't see any way of being able to refer to the new ViewController in code - any attempt to ctrl-drag a connection doesn't work.

即在 AppDelegate 中,我可以像这样访问基本的 ViewController

i.e. within the AppDelegate I can get to the base ViewController like this

(MyViewController*) self.window.rootViewController

但是故事板中包含的任何其他 ViewController 怎么样?

but how about any other ViewController contained within the storyboard?

推荐答案

查看文档 -[UIStoryboard instantiateViewControllerWithIdentifier:].这允许您使用您在 IB 属性检查器中设置的标识符从故事板实例化视图控制器:

Have a look at the documentation for -[UIStoryboard instantiateViewControllerWithIdentifier:]. This allows you to instantiate a view controller from your storyboard using the identifier that you set in the IB Attributes Inspector:

编辑添加示例代码:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];

MyViewController *controller = (MyViewController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"<Controller ID>"];

这篇关于Storyboard - 参考 AppDelegate 中的 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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