Can UIView be copied?

后端 未结 6 1600
广开言路
广开言路 2020-11-27 15:02

Simply using this way:

UIView* view2 = [view1 copy]; // view1 existed

This will cause simulator can not launch this app.

Try retai

6条回答
  •  [愿得一人]
    2020-11-27 15:40

    You can make method something like this:

    -(UILabel*)copyLabelFrom:(UILabel*)label{
    //add whatever needs to be copied
    UILabel *newLabel = [[UILabel alloc]initWithFrame:label.frame];
    newLabel.backgroundColor = label.backgroundColor;
    newLabel.textColor = label.textColor;
    newLabel.textAlignment = label.textAlignment;
    newLabel.text = label.text;
    newLabel.font = label.font;
    
    return [newLabel autorelease];
    
    }
    

    Then you can set your ivar to the return value and retain it like so:

    myLabel = [[self copyLabelFrom:myOtherLabel] retain];
    

提交回复
热议问题