iOS:如何将内容从滚动视图复制到另一个滚动视图

时间:2014-05-20 06:05:28

标签: ios objective-c uiscrollview uibutton

  • 以编程方式创建UIScrollView并在其中添加了多个buttons
  • 它没有任何问题。
  • 但我需要通过复制上一个scrollview中的内容来创建另一个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 

2 个答案:

答案 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];
}