【问题标题】:how to use multiple copies of any Subview(created in xib) from xib file如何使用来自 xib 文件的任何子视图(在 xib 中创建)的多个副本
【发布时间】:2013-08-27 12:31:45
【问题描述】:

我正在使用 xib 为我的项目创建视图。条件是:

我有多个UIView IBoutlet 的对象。

IBOutlet UIView *viewOpenDoor;
IBOutlet UIView *viewOpenDoor_Second;

viewOpenDoor 仅连接到 xib 中的视图之一。 现在我正在使用此代码在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];

它给出了例外-

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



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

所以,我的问题是如何重复使用在 xib 中创建的 IBOutlet 对象,多次使用不同的实例?

【问题讨论】:

  • 提前致谢,我正在等待某人的快速回复。
  • 我可以在该特定视图的 xib 文件中创建多个副本并使用它们,但我想知道多次重用一个对象的方法。

标签: iphone ios objective-c xcode uiview


【解决方案1】:

您将xib 加载到UINib 中,并从该UINib 实例化您想要的所有副本。

然后访问整个xib的副本的出口。您不能复制视图,您只能从同一个 UINib 实例化多个“副本”。

如果您打算稍后创建更多,您可以将UINib 存储在实例变量中。

【讨论】:

    【解决方案2】:

    试试这个:- 将UIView 的所有实例(您要连接的)引用到 XIB 文件,如下图所示。

    【讨论】:

    • 它将视图视为指向单个对象的多个实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多