UIView可以复制吗?

发布于 2024-10-07 00:21:08 字数 385 浏览 0 评论 0原文

简单地使用这种方式:

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

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

尝试保留,

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

view2 的任何修改都将应用于 view1,我知道 view2view1 共享相同的内存。

为什么UIView无法复制?原因是什么?

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.

Why can't UIView be copied? What is the reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不回头走下去 2024-10-14 00:21:08

这可能对您有用...存档视图,然后立即取消存档。这应该会给你一个视图的深层副本:

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

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]];
花间憩 2024-10-14 00:21:08

您的应用程序可能会因以下内容而崩溃:

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

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

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.

反话 2024-10-14 00:21:08

您可以制作 UIView 扩展。在下面的示例 swift 代码片段中,函数 copyView 返回一个 AnyObject,因此您可以复制 UIView 的任何子类, UIImageView。
如果您只想复制 UIView,您可以将返回类型更改为 UIView。

//MARK: - UIView Extensions

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

用法示例:

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

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()
西瑶 2024-10-14 00:21:08

对于 swift3.0.1:

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

for swift3.0.1:

extension UIView{
 func copyView() -> AnyObject{
    return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self))! as AnyObject
 }
}
窗影残 2024-10-14 00:21:08

UIView 没有实现 NSCoping 协议,请参阅 UIView.h 中的声明:

@interface UIView : UIResponder

因此,如果我们想要有一个类似 copy 的方法,我们需要实现 NSCoping 协议一个类别左右。

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.

丢了幸福的猪 2024-10-14 00:21:08

您可以将方法设置为如下所示:

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

}

然后您可以将 ivar 设置为返回值并保留它,如下所示:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文