ios UIView 可以复制吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4425939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:17:27  来源:igfitidea点击:

Can UIView be copied?

iosobjective-cuiviewcopy

提问by Forrest

Simply using this way:

简单地使用这种方式:

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

This will cause simulator can not launch this app.

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

Try retain,

尝试保留,

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

Any modifications to view2will apply to view1, I understand that view2share same memory with view1.

对 的任何修改都view2将适用于view1,我知道view2view1.

Why can't UIViewbe copied? What is the reason?

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

采纳答案by Engin Kurutepe

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 copyWithZoneselector in UIView.

原因是 UIView 没有实现复制协议,因此copyWithZoneUIView 中没有选择器。

回答by j2emanue

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

回答by Ivan Porkolab

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, ieUIImageView. If you want to copy onlyUIViews you can change the return type to UIView.

你可以做一个 UIView 扩展。在下面的示例 swift 片段中,函数 copyView 返回一个 AnyObject,因此您可以复制 UIView 的任何子类,UIImageView。如果你想复制UIViews你可以改变返回类型的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()

回答by Sour LeangChhean

for swift3.0.1:

对于 swift3.0.1:

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

回答by HongchaoZhang

UIViewdoesn't implement the NSCopingprotocol, see the declaration in UIView.h:

UIView不实现NSCoping协议,见UIView.h 中的声明:

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

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

So, if we want to have a copylike method, we need to implement the NSCopingprotocol in a category or so.

所以,如果我们想要一个copylike方法,我们需要NSCoping在一个类别中实现协议。

回答by ssj

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:

然后你可以将你的 ivar 设置为返回值并像这样保留它:

myLabel = [[self copyLabelFrom:myOtherLabel] retain];