开发者

Multiple XIBs same File's Owner

开发者 https://www.devze.com 2023-01-15 08:06 出处:网络
I am trying to create a two view, single controller application as follows: I have two XIB\'s. Each with the same File\'s Owner.

I am trying to create a two view, single controller application as follows: I have two XIB's. Each with the same File's Owner.

As a test, I have placed a UILabel on each XIB. I have connected the File Owner to the UILabel in each XIB. The outlet property is the same.

When I instantiate the nib using loadNibNamed I also set the 'owner' to the instance of File's Owner, e.g.:

nib=[[NSBundle mainBundle] loadNibNamed:@"ONE" owner:OWNER options:nil];
nib=[[NSBundle mainBundle] loadNib开发者_运维知识库Named:@"TWO" owner:OWNER options:nil];

Now, in OWNER, if I call

[myLabel setText:@"Hello World"];

I see the label update only in nib TWO.

If I create additional UILabels that are unique to each NIB then I can properly update and view them. It seems that I can only have one connection from the property on File's Owner to each NIB.

Any ideas?


What you want is an IBOutletCollection. That allows you to assign a property to more than one nib element, and talk about the entire group all at once.


an IBOutlet can only point to one object. You will need two of every IBOutlet you want to use.


This is very old and not much viewed, but I can't help but notice why this doesn't work. You're passing in the same instance of the owner. Make two instances of the file's owner and you can have two different label values. There's no reason you couldn't assign all of your outlets in your whole project to one Object class in fact, although you probably wouldn't want to do this. Another thing to think about is whether you shouldn't be using inheritance here, by making a superclass, connecting all of the common outlets to that class and then a subclass with unique outlets. Indeed, since you will either have to distinguish your nibs by calling them by nib identifier or by a the class associated with them I think it's better practice to associate separate classes and use inheritance to cover the overlap between them.

0

精彩评论

暂无评论...
验证码 换一张
取 消