简体   繁体   中英

iOS: How to copy contents from a scrollview into another scrollview

  • Am created UIScrollView programatically and added multiple buttons into that.
  • Its working well without any prob.
  • But i need to create another scrollview by copying contents from previous scrollview.

Query: How to copy First_Scrollview content into Second_Scrollview

Code:

- (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
}

Error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScrollView copyWithZone:]: unrecognized selector sent to instance 

You can't copy views in the way you are trying.

You need to do it as :

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

you can do this by this for loop also

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM