高效地使用Interface Builder [英] Using Interface Builder efficiently

查看:153
本文介绍了高效地使用Interface Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的iPhone和目标c。我花了几个小时和几个小时阅读文件,并试图了解事情如何运作。我有RTFM或至少在进行中。



我的主要问题是我想了解如何指定事件传递到哪里,唯一的方法能够做到这一点是通过指定代表,但我确定在IB有一个更容易/更快捷的方式。



所以,一个例子。
说我有20个不同的视图和视图控制器和一个MyAppDelegate。
我希望能够在IB中构建所有这些不同的Xib文件,然后添加许多按钮和文本字段以及任何内容,然后指定它们都在MyAppDelegate对象中生成一些事件。为此,我在IB列表视图中的每个视图控制器中添加了一个MyAppDelegate对象。然后我在XCode中的MyAppDelegate中创建了一个IBAction方法,并返回到IB,并将所有事件链接到每个Xib文件中的MyAppDelegate对象。



但是,当我尝试运行它时,它只是遇到一个坏的读取异常。



我的猜测是, Xib文件放置一个MyAppDelegate对象指针,与实际在运行时创建的MyAppDelegate地址无关。



所以我的问题是...如何我这样做?!!!

解决方案

如果您在每个nib文件中创建一个MyAppDelegate的实例,那么是的,你会结束当所有的笔尖加载时,都有很多不同的类的实例。应用程序委托不是通过类或甚至协议来标识,而是由应用程序实例的委托属性指向的对象。要找到真正的应用程序代理,您必须向应用程序对象本身询问其代理权限。



您应该让所有的视图控制器都从父视图控制器类一个 appDelegate 属性。执行类似这样的操作:

  #importMyAppDelegateClass.h

@interface ViewControllerBaseClass:UIViewController {
MyAppDelegateClass * appDelegate;
}
@property(nonatomic,retain)* appDelegate;

@end

@implementation ViewControllerBaseClass
@synthesize appDelegate;

- (MyAppDelegateClass *)appDelegate {
self.appDelegate =(MyAppDelegateClass *)[[UIApplication sharedInstance] delegate];
return appDelegate;
}
@end

当视图控制器需要应用程序委托它时调用 self.appDelegate 。如果要访问应用程序委托的属性,请使用 self.appDelegate.attributeName



重要的是,您可以在运行时向应用程序询问其特定代理实例。您不能从nib文件中执行此操作。


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?!!!

解决方案

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 delegate

You should have all your view controllers descend from a parent view controller class that has an appDelegate property. Implement something like this:

#import "MyAppDelegateClass.h"

@interface ViewControllerBaseClass :UIViewController {
    MyAppDelegateClass *appDelegate;
}
@property(nonatomic, retain)  *appDelegate;

@end

@implementation ViewControllerBaseClass
@synthesize appDelegate;

-(MyAppDelegateClass *) appDelegate{
    self.appDelegate=(MyAppDelegateClass *)[[UIApplication sharedInstance] delegate];
    return appDelegate;
}
@end

When the view controller needs the app delegate it just calls self.appDelegate. If you want to access an attribute of the app delegate use self.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.

这篇关于高效地使用Interface Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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