objective-c - 复制 UIView

标签 objective-c ios cocoa-touch

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

我在 .xib 文件中制作了带有标签和按钮的 UIView 。现在我希望仅使用不同的标签文本复制此 View n 次。

我正在尝试这样做,但这样我只能得到显示的最后一个对象。 _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];
    }

我想你会明白我想要做什么,也许我这样做的想法是完全错误的,所以有人可以帮助我走上这条路吗?

最佳答案

从 Nib 多次加载 View ,调整加载的每个副本上的标签内容。与尝试将 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 实例重复上述代码即可。

关于objective-c - 复制 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698993/

相关文章:

ios - Sprite Kit 停止播放音乐文件并从头开始播放

ios - Swift - 以编程方式创建 View 问题,无法找到存储的属性

iphone - 区分 didSelectRowAtIndexPath 和accessoryButtonTappedForRowWithIndexPath

objective-c - 无法获取导航栏左侧的分段控件

ios - 如何让 uitableview 一次加载多个部分?

objective-c - 使用 Application Loader 上传 Mac 应用程序

ios - 如何使用 swift 执行SelectorOnMainThread 并在不使用闭包的情况下获取返回值?

iphone - 在我的 UIView 中每 5 分钟调用一次方法

ios - 如果编译时间超过可接受的水平,我可以创建一个失败的单元测试吗?

ios - 如何隐藏 MPmoviePlayerController 控制栏