Objective c 复制UIView

Objective c 复制UIView,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我不知道如何复制UIView及其内容。或者甚至有可能吗 我在.xib文件中制作了一个标签和一个按钮,并在其中创建了一个UIView。现在我只想用不同的标签文本复制该视图n次 我试着这样做,但这样我只能看到最后一个物体\u view1是IBOutlet就像\u view1Label一样 NSMutableArray *Info = [[NSMutableArray alloc]initWithCapacity:number.intValue]; for(int i = 0; i !

我不知道如何复制
UIView
及其内容。或者甚至有可能吗

我在.xib文件中制作了一个标签和一个按钮,并在其中创建了一个
UIView
。现在我只想用不同的标签文本复制该视图n次

我试着这样做,但这样我只能看到最后一个物体
\u view1
IBOutlet
就像
\u view1Label
一样

    NSMutableArray *Info = [[NSMutableArray alloc]initWithCapacity:number.intValue];
    for(int i = 0; i != number.intValue; i++)
    {
        NSString *number = [NSString stringWithFormat:@"Zona%i ",[[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"].intValue];
        NSString *zona = [NSString stringWithFormat:@"%@%i",number, i+1];
        NSString *parinkta = [[NSUserDefaults standardUserDefaults] stringForKey:zona];
        _view1Label.text = parinkta;
        _view1.hidden = false;
        CGRect newrect = CGRectMake(_view1.frame.origin.x, _view1.frame.origin.y + (80 * i), _view1.frame.size.width, _view1.frame.size.height);
        _view1.frame = newrect;
        [Info addObject:_view1];
    }
    for(int i = 0; i != number.intValue; i++)
    {
        UIView *add = [Info objectAtIndex:i];
        [self.view addSubview:add];
    }

我想你会明白我想做什么,也许我这样做的想法是完全错误的,那么有人能帮我找到这条路吗?

从笔尖多次加载视图,调整加载的每个副本上的标签内容。这比尝试在内存中复制UIView内容要简单、短得多,也不容易出错

下面是使用
UINib
访问nib内容的示例:

 UINib *nib = [UINib nibWithNibName:@"nibName" bundle:nil];
 NSArray *nibContents = [nib instantiateWithOwner:nil options:nil];

 // Now nibContents contains the top level items from the nib.
 // So a solitary top level UIView will be accessible
 // as [nibContents objectAtIndex:0]

 UIView *view = (UIView *)[nibContents objectAtIndex:0];

 // assumes you've set the tag of your label to '1' in interface builder
 UILabel *label = (UILabel *)[view viewWithTag:1];
 label.text = @"My new text";

因此,只需为您想要的每个nib实例重复上述代码。

如果您希望显示
n
视图,那么您必须创建视图
n
次,在第一个for循环内,您不能在多个位置放置单个视图

如果您是通过代码创建第一个视图,那么您可以使用以下方法

将for循环更改为:

for(int i = 0; i != number.intValue; i++)
    {
        NSString *number = [NSString stringWithFormat:@"Zona%i ",[[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"].intValue];
        NSString *zona = [NSString stringWithFormat:@"%@%i",number, i+1];
        NSString *parinkta = [[NSUserDefaults standardUserDefaults] stringForKey:zona];
        UIView *tempView = [[UIView alloc] init];
        UILabel *tempLabel = [[UILabel alloc] init];
        tempLabel.frame = _view1Label.frame;
        tempLabel.text = _view1Label.text;
        [tempView addSubview: tempLabel];
        tempView.frame = _view1.frame;
        _view1Label.text = parinkta;
        _view1.hidden = false;
        CGRect newrect = CGRectMake(_view1.frame.origin.x, _view1.frame.origin.y + (80 * i), _view1.frame.size.width, _view1.frame.size.height);
        _view1.frame = newrect;
        [Info addObject:tempView];
    }

请使用
UINib
有效地执行此操作。您能否更具体地说明如何执行此操作?我很抱歉我缺乏知识,简单的例子可以让添加小例子的答案更加清晰。如果您想了解更多,请阅读API文档!这可能会解决他发布的代码的问题,但也会鼓励完全错误的方法。