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
Can UIView be copied?
提问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 view2
will apply to view1
, I understand that view2
share same memory with view1
.
对 的任何修改都view2
将适用于view1
,我知道view2
与view1
.
Why can't UIView
be 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 copyWithZone
selector in UIView.
原因是 UIView 没有实现复制协议,因此copyWithZone
UIView 中没有选择器。
回答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
UIView
doesn't implement the NSCoping
protocol, 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 copy
like method, we need to implement the NSCoping
protocol in a category or so.
所以,如果我们想要一个copy
like方法,我们需要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];