【发布时间】:2014-05-20 06:05:27
【问题描述】:
- 我以编程方式创建了
UIScrollView,并在其中添加了多个buttons。 - 它运行良好,没有任何问题。
- 但我需要通过从以前的滚动视图中复制内容来创建另一个
scrollview。
查询:
如何将First_Scrollview内容复制到Second_Scrollview中
代码:
- (void)ScrollviewItems
{
int x = 5;
int y = 5;
for(int i=0; i<totalButtonsCount; i++)
{
CategoryItem = [[UIButton alloc]initWithFrame:CGRectMake(x, y, buttonWidth, 50)];
CategoryItem.titleLabel.backgroundColor = [UIColor clearColor];
CategoryItem.backgroundColor = [UIColor redColor];
[CategoryItem setTitle:[NSString stringWithFormat:@"%d",i]
forState:UIControlStateNormal];
CategoryItem.tag = i;
[CategoryItem addTarget:self
action:@selector(CategoryItemAction:)
forControlEvents:UIControlEventTouchUpInside];
[First_Scrollview addSubview:CategoryItem];
x = x + (buttonWidth + 5);
}
First_Scrollview.contentSize = CGSizeMake(x,50);
Second_Scrollview = [self.Category_Scrollview copy]; // Error
}
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScrollView copyWithZone:]: unrecognized selector sent to instance
【问题讨论】:
-
[UIScrollView copy]不能这样工作。实现目标的最简单方法是循环滚动视图的子视图并将这些子视图添加到目标滚动视图。 -
UIView 可以在一个超级视图/父视图中。要复制它,您需要重新创建它并将其作为子视图添加到第二个父视图中,迭代就像 Raptor 建议的那样是您的朋友。
标签: ios objective-c uiscrollview uibutton