【问题标题】:iOS: How to copy contents from a scrollview into another scrollviewiOS:如何将内容从滚动视图复制到另一个滚动视图
【发布时间】: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


【解决方案1】:

你也可以通过这个for循环来做到这一点

for(UIView *v in self.scrollview1.subviews){
        [self.scrollview2 addSubview:v];
}

【讨论】:

    【解决方案2】:

    您不能以您尝试的方式复制视图。

    你需要这样做:

    NSData * viewData = [NSKeyedArchiver archivedDataWithRootObject:myView];
    NSView * viewCopy = [NSKeyedUnarchiver unarchiveObjectWithData:viewData];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2017-10-05
      • 1970-01-01
      相关资源
      最近更新 更多