简体   繁体   English

在Objective-C中复制UI元素

[英]Copy UI element in Objective-C

I'm trying to customize the UIImagePickerController for iPhone right now. 我正在尝试自定义iPhone的UIImagePickerController。 I could modify the default buttons (take and cancel) as well as add more buttons. 我可以修改默认按钮(接受和取消)以及添加更多按钮。 But I have trouble when attempting to make the customized button look-and-feel like the default iPhone's ones. 但是,当尝试使自定义按钮的外观和默认iPhone的外观相似时,我遇到了麻烦。 At first I think that it'd be possible if I get and then clone the default button as a template for mine by the following code 起初,我认为可以通过以下代码获得然后克隆默认按钮作为我的模板

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];

But it didnt work due to the fact I have just known lately that NSObject and its subclasses dont implement the protocol NSCopying by default. 但是由于我最近才知道NSObject及其子类在默认情况下不实现NSCopying协议,因此它不起作用。

I wonder is there any way to either - copy an UI element and then modify it as my will - create a custom button with the look of Apple's ones 我想知道有什么方法可以-复制UI元素,然后按我的意愿对其进行修改-创建具有Apple外观的自定义按钮

If you want to duplicate any UIView, put it in a nib file, and load it as many times as you want. 如果要复制任何UIView,请将其放在nib文件中,并根据需要加载多次。

Another alternative is to archive an existing view and then unarchive it. 另一种选择是存档现有视图,然后取消存档。 See the NSKeyedArchiver class. 请参阅NSKeyedArchiver类。

id theButton;  //whatever way you obtain this...
id copyOfTheButton = [NSKeyedUnarchiver unarchiveObjectWithData:
                     [NSKeyedArchiver archivedDataWithRootObject: theButton]];

There is no easy way to copy an Objective-C object that does not implement NSCopying (unless you use NSResponder's suggestion of using NSCoding, in which case there is.) There is NSCopyObject() , but this is perhaps the most dangerous function in all of Foundation (see NSCopyObject() considered harmful for my thoughts on this insane function). 有没有简单的方法来复制一个Objective-C的对象没有实现 NSCopying (除非你使用使用NSCoding的NSResponder类的建议,在这种情况下有。)NSCopyObject()但是这也许是所有最危险的功能基础知识(请参阅NSCopyObject()被认为对我对此疯狂功能的想法有害 )。

Your options are safe or dangerous. 您的选择是安全或危险的。 The safe way is to just copy the look and feel of the element in your own code. 安全的方法是只在自己的代码中复制元素的外观。 This is tedious, but generally achievable. 这是乏味的,但是通常是可以实现的。 The dangerous way is to reverse engineer the element's class and interface and try to create your own instance (with alloc/init). 危险的方法是对元素的类和接口进行反向工程,然后尝试创建自己的实例(使用alloc / init)。 This might work and it might not, and it may break between versions of the OS, but it may give you better results, faster. 这可能会起作用,但可能不会起作用,并且可能会在操作系统版本之间中断,但可能会更快地为您带来更好的结果。 Personally, I tend towards just recreating the original element by hand as best you can. 就个人而言,我倾向于尽可能地手动重建原始元素。 Generally Photoshop is an important part of that work. 通常,Photoshop是该工作的重要部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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