简体   繁体   English

如何强制接口构建器(storyboard)生成控制器初始化代码?

[英]How to force interface builder (storyboard) to generate controller initialisation code?

I am getting incredibly frustrated with interface builder at the moment, and would appreciate some help before I ragequit it and code everything by hand (which seems to be much, much, much easier). 目前,我对接口生成器感到非常沮丧,在我对它进行修改和手工编写所有代码之前,希望能获得一些帮助(这似乎非常容易得多)。

The basic situation is this: I need to make a model variable accessible to each view controller in my application. 基本情况是这样的:我需要使应用程序中的每个视图控制器都可以访问模型变量。

The simplest way I can see to do this is to just create a property on the view controllers that retains the model, and to set that after the controller is initialised. 我看到的最简单的方法是在视图控制器上创建一个保留模型的属性,并在控制器初始化后对其进行设置。

However, I can't find any of the actual initialisation code for the views shown on the storyboard in my project. 但是,我找不到项目中故事板上显示的视图的任何实际初始化代码。 There's no reference to any of them at all. 根本没有任何提及。 Does the interface builder really generate not generate any code reference to its controllers in the app delegate? 接口构建器是否真的在应用程序委托中不生成对其控制器的任何代码引用?

For that matter, why is there no reference to any of the top level controller objects (tabview, tableview etc) in code at all? 因此,为什么在代码中根本没有引用任何顶级控制器对象(选项卡视图,表视图等)?

All I want to know is how to force xcode to actually generate the controller creation code in AppDelegate.m - so that I have access to the created instance of the controller - or, failing that, a way to share the model between these amorphous objects. 我想知道的是如何强制xcode在AppDelegate.m中实际生成控制器创建代码-以便我可以访问控制器的已创建实例-否则,将无法在这些无定形对象之间共享模型。

You will need to manually create a subclass of your view controller and then override the methods you want to inject code into. 您将需要手动创建视图控制器的子类,然后覆盖要向其中注入代码的方法。 In Interface Builder you can then choose to make your View controllers of this custom type. 然后,您可以在Interface Builder中选择使这种自定义类型的View控制器成为可能。

Maybe it would be easier to create a singleton class where you can store all your global variables and methods. 创建一个单例类可能会更容易,您可以在其中存储所有全局变量和方法。 Example here . 这里的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在从界面构建器中的Storyboard场景实例化的视图控制器上调用自定义init? - How do I call a custom init on a view controller instantiated from a Storyboard scene in interface builder? 如何使用Interface Builder中的Storyboard连接页面视图控制器的dataSource出口 - How to connect dataSource outlet of a Page View Controller using Storyboard in Interface Builder 在Interface Builder故事板上创建UIView - Create UIView in Interface Builder Storyboard 如何使用Interface Builder添加导航控制器? - How to add a Navigation Controller with Interface Builder? 如何在Interface Builder中的Navigation Controller中调整子视图的大小 - How to resize sub views in Navigation Controller in Interface Builder 如何在Interface Builder故事板中跨场景连接IBOutlet? - How do you hook up IBOutlets across scenes in the Interface Builder storyboard? 强制乘数更新Interface Builder约束 - Force multiplier to update on Interface Builder constraint 如何在storyboard创建的标签栏控制器上编写代码? - How to write code on a tab bar controller created by storyboard? 将Interface Builder代码转换为Xcode - Convert Interface Builder code to Xcode Interface Builder无法确定“ Main_iPad.storyboard”的类型 - Interface Builder was unable to determine the type of “Main_iPad.storyboard”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM