如何在Objective C中复制UIButton? [英] How to duplicate a UIButton in Objective C?

查看:93
本文介绍了如何在Objective C中复制UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该对象继承自NSObject。

The object inherits from NSObject.

是否有方法将其副本创建为新对象?

Is there a method to create a copy of it as a new object?

推荐答案

UIButton不符合NSCopying,因此您无法通过-copy制作副本。

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

但是,它确实符合NSCoding,因此您可以归档当前实例,然后取消归档副本。

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

NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: button];
UIButton *buttonCopy = [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.

这篇关于如何在Objective C中复制UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆