高效使用 Interface Builder
我是 iPhone 和 Objective c 的新手。我花了很多时间阅读文档并试图理解事情是如何运作的。我有 RTFM,或者至少正在这个过程中。
我的主要问题是我想了解如何指定事件传递到的位置,而我能够做到这一点的唯一方法是指定委托,但我确信 IB 中有一种更简单/更快的方法。
那么,举个例子。 假设我有 20 个不同的视图和视图控制器以及一个 MyAppDelegate。 我希望能够在 IB 中构建所有这些不同的 Xib 文件,并添加任意数量的按钮和文本字段等,然后指定它们都在 MyAppDelegate 对象中生成一些事件。为此,我在 IB 列表视图的每个视图控制器中添加了一个 MyAppDelegate 对象。然后,我在 XCode 中的 MyAppDelegate 中创建了一个 IBAction 方法,然后返回 IB,并将所有事件链接到每个 Xib 文件中的 MyAppDelegate 对象。
然而,当我尝试运行它时,它只是因错误读取异常而崩溃。
我的猜测是,每个 Xib 文件都放置一个 MyAppDelegate 对象指针,该指针与运行时实际创建的最终 MyAppDelegate 地址无关。
所以我的问题是...我该怎么做?
I am new to iPhone and objective c. I have spent hours and hours and hours reading documents and trying to understand how things work. I have RTFM or at least am in the process.
My main problem is that I want to understand how to specify where an event gets passed to and the only way I have been able to do it is by specifying delegates but I am certain there is an easier/quicker way in IB.
So, an example.
Lets say I have 20 different views and view controllers and one MyAppDelegate.
I want to be able to build all of these different Xib files in IB and add however many buttons and text fields and whatever and then specify that they all produce some event in the MyAppDelegate object. To do this I added a MyAppDelegate object in each view controller in IB's list view. Then I created an IBAction method in MyAppDelegate in XCode and went back to IB and linked all of the events to the MyAppDelegate object in each Xib file.
However when I tried running it it just crashed with a bad read exception.
My guess is that each Xib file is putting a MyAppDelegate object pointer that has nothing to do with the eventual MyAppDelegate adress that will actually be created at runtime.
So my question is...how can I do this?!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在每个 nib 文件中创建一个 MyAppDelegate 实例,那么,是的,当所有 nib 加载时,您最终会得到该类的许多不同实例。应用程序委托不是通过类甚至协议来标识的,而是通过应用程序实例的
delegate
属性指向的对象来标识的。要找到真正的应用程序委托,您必须向应用程序对象本身询问其委托。您应该让所有视图控制器都继承自具有
appDelegate
属性的父视图控制器类。实现如下:当视图控制器需要应用程序委托时,它只需调用 self.appDelegate 。如果您想访问应用程序委托的属性,请使用 self.appDelegate.attributeName 。
重要的是您在运行时向应用程序询问其特定的委托实例。你不能从 nib 文件中做到这一点。
If you create an instance of MyAppDelegate in each nib file then, yes, you do end up with a lot of different instances of the class when all the nibs load. The app delegate is not identified by class or even protocol but rather by being the object pointed to by the application instance's
delegate
property. To find the true app delegate, you have have to ask the application object itself for its delegateYou should have all your view controllers descend from a parent view controller class that has an
appDelegate
property. Implement something like this:When the view controller needs the app delegate it just calls
self.appDelegate
. If you want to access an attribute of the app delegate useself.appDelegate.attributeName
.The important thing is that you ask the application for its specific delegate instance at runtime. You can't do that from a nib file.
我不完全清楚你到底想做什么,但这可能是一个坏主意。每个应用程序应该只有一个应用程序委托,并且它应该处理整个应用程序的行为。通常,应用程序委托会初始化根视图控制器并显示它们,但不会做太多其他事情(除了处理打开和保存数据源之类的事情)。
视图控制器(
UIViewController
的子类)应与 XIB 交互。在视图控制器中具有特定于视图的行为使应用程序更易于管理和维护。通常,每个视图控制器应该有 0 或 1 个 XIB(超过这个数量就很复杂)。您可以使用 IBOutlet 和 IBActions 的目标/操作模式来设置与视图的交互(请参阅 此处 获取完整指南)。让视图控制器或 XIB 依赖于应用程序委托通常是一个坏主意(因为减少依赖关系再次使代码更易于管理)。I'm not entirely clear what exactly you're trying to do, but it's probably a bad idea. There should only be one app delegate per application, and it should deal with behavior for the whole application. Typically, the app delegate initializes the root view controller(s) and displays them, but not much else (other than handling things like opening and saving data sources).
The view controllers (subclasses of
UIViewController
) should interact with the XIBs. Having the view-specific behavior in the view controllers makes the app much easier to manage and maintain. Typically, there should be 0 or 1 XIBs per view controller (more than that is complicated). You set up the interaction with the views using the Target/Action pattern with IBOutlets and IBActions (see here for a complete guide). It's generally a bad idea to make view controllers or XIBs dependent on the app delegate (since reducing dependencies again makes the code easier to manage).一般来说,您应该为正在构建的每个视图创建一个视图控制器,并将事件链接到这些视图控制器 - 而不是应用程序委托。事实上,通常没有任何事件从任何 nib 文件连接到应用程序委托,即使在示例项目中,您也会注意到视图控制器是由应用程序委托创建并保存的,但它不接收事件。
In general you should be making a view controller for each of the views you are building, and link events to those view controllers - not the app delegate. In fact usually no event ever is wired to the app delegate from any nib file, even in the sample projects you'll note that view controllers are created and held onto by the app delegate, but it does not receive events.