简体   繁体   English

复制UIView

[英]Duplicating UIView

I can't figure out how could I duplicate UIView with its contents. 我不知道如何复制UIView及其内容。 Or is it even possible? 甚至有可能吗?

I have made and UIView with a label and a button inside a .xib file. 我已经在.xib文件中添加了带有标签和按钮的UIView Now I wish to copy this view n times only with different label text. 现在,我希望仅使用不同的标签文本将视图复制n次。

I'm trying to do this like this, but such way I only get the last object shown. 我正在尝试这样做,但是这样我只能得到显示的最后一个对象。 _view1 is IBOutlet just like _view1Label . _view1IBOutlet ,就像_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];
    }

I guess you'll get the idea what I am trying to do, maybe my idea of doing this is totaly wrong, so could anyone help me get on the path on this ? 我想您会知道我要做什么,也许我做这件事的想法是完全错误的,所以有人可以帮助我走上这个道路吗?

Load the view multiple times from the nib, adjusting the label contents on each copy you load. 从笔尖多次加载视图,从而调整每次加载的副本上的标签内容。 This is much easier, shorter, less prone to error than trying to copy the UIView contents in-memory which you're trying to do. 与尝试在内存中复制UIView内容相比,这更容易,更短,更不容易出错。

Here's an example of using UINib to access the nib contents: 这是使用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";

So just repeat the above code for each nib instance you want. 因此,只需对所需的每个笔尖实例重复以上代码。

如果要显示n视图,则必须创建n次视图,在第一个for循环中,不能将单个视图放置在多个位置

If you are created the first view through code then you can use the following method. 如果通过代码创建了第一个视图,则可以使用以下方法。

Change your for loop like: 更改您的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];
    }

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

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