UIView 可以复制吗?

简单地使用这种方法:

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

这将导致模拟器无法启动这个应用程序。

试着记住,

UIView* view2 = [view1 retain]; // view1 existed
// modify view2 frame etc

view2的任何修改都将适用于 view1,我了解到 view2view1共享相同的内存。

为什么不能复制 UIView? 原因是什么?

48832 次浏览

Your app probably crashes with something like:

 [UIView copyWithZone:]: unrecognized selector sent to instance 0x1c6280

The reason is that UIView does not implement the copying protocol, and therefore there is no copyWithZone selector in UIView.

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

this might work for you ... archive the view and then unarchive it right after. This should give you a deep copy of a view:

id copyOfView =
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:originalView]];

You can make an UIView extension. In example swift snippet below, function copyView returns an AnyObject so you could copy any subclass of an UIView, ie UIImageView. If you want to copy only UIViews you can change the return type to UIView.

//MARK: - UIView Extensions


extension UIView
{
func copyView<T: UIView>() -> T {
return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
}
}

Example usage:

let sourceView = UIView()
let copiedView = sourceView.copyView()

UIView doesn't implement the NSCoping protocol, see the declaration in UIView.h:

@interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment>

So, if we want to have a copy like method, we need to implement the NSCoping protocol in a category or so.

for swift3.0.1:

extension UIView{
func copyView() -> AnyObject{
return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self))! as AnyObject
}
}