【问题标题】:Swift remove reference to UIViewSwift 删除对 UIView 的引用
【发布时间】:2017-07-04 02:06:23
【问题描述】:

我想在点击时完全删除对 UIView 的引用(它是一个子视图。

@IBAction private func handleTap(_ sender: UITapGestureRecognizer) {
    if (delegate != nil) {
        delegate?.imageWasTapped(imageView.image!, photoURL: photoURL!)
        self.removeFromSuperview()
    }
    print ("tap")
}

removeFromSuperview() 似乎不足以完全删除实例(从我读过的内容中),所以想

self = nil

当然 self 是不可变的。

我试图将值作为 inout 参数传递给我的 superview,但当然 self 仍然是不可变的。

        delegate?.imageWasTapped(imageView.image!, photoURL: photoURL!, imageView: self)

removeFromSumerview() 是否足以从内存中释放 UIView,或者如何最好地完成?

【问题讨论】:

  • 当您使用 ARC 时,请让系统为您管理内存。

标签: ios swift memory uiview


【解决方案1】:

您可以对 UIView 进行弱引用。那么在你使用removeFromSuperview 删除它之后,UIView 将在 ARC 中为零。

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 2020-10-29
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多