How do copy for UILabel?

后端 未结 5 1456
星月不相逢
星月不相逢 2020-12-03 08:21

I have IBOutlet UILabel *label;

and I want to do this

UILabel *label = [titleLabel copy];
label.text = @\"Clone\";
titleLabel.text = @         


        
5条回答
  •  被撕碎了的回忆
    2020-12-03 08:24

    UILabel does not conform to NSCopying, so you cannot make a copy via -copy.

    But it does conform to NSCoding, so you can archive the current instance, then unarchive a 'copy'.

    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: label];
    UILabel *labelCopy =   [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
    

    Afterwards, you'll have to assign any additional properties that weren't carried over in the archive (e.g. the delegate) as necessary.

提交回复
热议问题