复制Objective-C中的UI元素

Kha*_*yen 2 iphone objective-c

我正在尝试为iPhone定制UIImagePickerController.我可以修改默认按钮(取消和取消)以及添加更多按钮.但是在尝试使自定义按钮看起来像默认的iPhone时,我遇到了麻烦.首先,我认为如果我通过以下代码获取然后克隆默认按钮作为我的模板是可能的

UIButton *cancelButton = [[[[[[[[[[[[[[[[self.view subviews] objectAtIndex:0]
                            subviews] objectAtIndex:0]
                            subviews] objectAtIndex:0]
                            subviews] objectAtIndex:0]
                            subviews] objectAtIndex:2]
                            subviews] objectAtIndex:0]
                            subviews] objectAtIndex:1]
                            subviews] objectAtIndex:1];


UIButton *anotherButton = [cancelButton copy];
Run Code Online (Sandbox Code Playgroud)

但由于我最近才知道NSObject及其子类默认情况下不实现NSCopying协议,所以它没有用.

我想知道有没有办法 - 复制一个UI元素,然后按照我的意愿修改它 - 创建一个带有Apple的外观的自定义按钮

NSR*_*der 5

如果要复制任何UIView,请将其放在nib文件中,并根据需要多次加载.

另一种方法是归档现有视图,然后取消归档.请参阅NSKeyedArchiver类.

id theButton;  //whatever way you obtain this...
id copyOfTheButton = [NSKeyedUnarchiver unarchiveObjectWithData:
                     [NSKeyedArchiver archivedDataWithRootObject: theButton]];
Run Code Online (Sandbox Code Playgroud)

  • 这真的是获取对象副本的最佳实践方法吗?我理解机制,但似乎很多工作必须在幕后进行,看似比较简单的操作. (2认同)