XCode Interface Builder中的对象与外部对象

时间:2013-02-28 19:05:27

标签: ios xcode interface-builder

IB中的对象和外部对象有什么区别?
我什么时候应该使用?

2 个答案:

答案 0 :(得分:6)

添加到另一个答案:  您可以使用“外部对象”来跨多个xib访问公共对象。你也可以用其他方式做到这一点,但这很方便。

例如,如果您对多个xib上的按钮点击执行“大”操作,并且您有许多此类操作(此外,如果您执行此操作的数据相同),而不是调用addTarget:action...,您可以创建此类的代理对象并将其连接到按钮。

您可以使用以下代码将代理对象连接到xib:

 id *proxy = <someObject>; //The object you want to wire up
//In the below line of code use the same key as the identifier you give for the proxy object in the Interface Builder
 UINib *nib = [UINib nibWithNibName:@"ViewController" bundle:Nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:proxyObject,@"proxy", nil];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:dict,UINibExternalObjects, nil];
NSArray  *nibArray = [nib instantiateWithOwner:self options:dict2];
self.view = [nibArray objectAtIndex:0];

答案 1 :(得分:3)

对象实际上是嵌入在笔尖中的东西。

外部对象是加载nib的代码承诺在加载时提供的代码(我相信通过将键映射到外部对象的字典)。

除了文件所有者(已经为您提供)之外,大多数人从不使用任何外部对象。你几乎肯定只想要对象。