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

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

问题描述

考虑以下场景:我有一个基于故事板的应用程序。我将一个ViewController对象添加到storyboard,将此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?

推荐答案

查看的[apple_ref / doc / uid / TP40010909-CH1-SW4rel =noreferrer>文档 - [UIStoryboard instantiateViewControllerWithIdentifier:] 。这允许您使用在IB Attributes Inspector中设置的标识符从故事板中实例化视图控制器:

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

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

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