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
答案 0 :(得分:1)
您无法以您尝试的方式复制视图。
你需要这样做:
NSData * viewData = [NSKeyedArchiver archivedDataWithRootObject:myView];
NSView * viewCopy = [NSKeyedUnarchiver unarchiveObjectWithData:viewData];
答案 1 :(得分:1)
你可以通过这个for循环来做到这一点
for(UIView *v in self.scrollview1.subviews){
[self.scrollview2 addSubview:v];
}