简体   繁体   English

如何使用xib文件中的任何Subview(在xib中创建)的多个副本

[英]how to use multiple copies of any Subview(created in xib) from xib file

I am using xib to create view for my project. 我正在使用xib为我的项目创建视图。 The condition is: 条件是:

I have multiple UIView IBoutlet's Object. 我有多个UIView IBoutlet的对象。

IBOutlet UIView *viewOpenDoor;
IBOutlet UIView *viewOpenDoor_Second;

viewOpenDoor is only connected to one of the view in xib. viewOpenDoor仅连接到viewOpenDoor中的一个视图。 Now i am using this code to reuse the same view multiple times in viewdidload method- 现在我使用此代码在viewdidload方法中多次重用相同的视图 -

[viewOpenDoor setFrame:CGRectMake(30, 80, viewOpenDoor.frame.size.width, viewOpenDoor.frame.size.height)];
[self.view addSubview:viewOpenDoor];
viewOpenDoor.layer.borderColor = [UIColor blackColor].CGColor;
viewOpenDoor.layer.borderWidth = 0.9f;
viewOpenDoor.layer.cornerRadius = 6.0f;

 [viewOpenDoor setHidden:YES];

viewOpenDoor_Second = [[UIView alloc] init];
viewOpenDoor_Second = [viewOpenDoor copy];

[viewOpenDoor_Second setFrame:CGRectMake(184, 80, viewOpenDoor.frame.size.width, viewOpenDoor.frame.size.height)];

[self.view addSubview:viewOpenDoor_Second];

it's giving exception- 它给予例外 -

-[UIView copyWithZone:]: unrecognized selector sent to instance 0x95ba140



Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView copyWithZone:]: unrecognized selector sent to instance 0x95ba140'

So, my question is how can i reuse this one IBOutlet object created in xib, multiple times with different instances? 所以,我的问题是如何重用这个在xib中创建的IBOutlet对象,多次使用不同的实例?

You load the xib into a UINib and instantiate all the copies you want from that UINib . 您将xib加载到UINib并从该UINib实例化所需的所有副本。

Then access the outlet of the copy of the whole xib . 然后访问整个xib副本的出口。 You cant copy a view, you can only instatiate multiple "copies" from the same UINib . 您无法复制视图,只能从同一个UINib实例化多个“副本”。

You can store the UINib in an instance variable if you plan to create more later. 如果计划稍后创建更多,则可以将UINib存储在实例变量中。

Try this:- Reference all the instances(that you want to connect) of UIView to XIB file as shown in below image. 试试这个: - 将UIView所有实例(你要连接的)引用到XIB文件,如下图所示。

在此输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM