我不知道如何复制 UIView
及其内容。或者说有可能吗?
我在 .xib 文件中制作了带有标签和按钮的 UIView
。现在我希望仅使用不同的标签文本复制此 View n 次。
我正在尝试这样做,但这样我只能得到显示的最后一个对象。 _view1
是 IBOutlet
,就像 _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/