调用 removeFromSuperview 后如何防止子视图释放自身?

How to prevent subview release self after call removeFromSuperview?

我想用两个子视图来改变,因为按钮被点击了,子视图是由故事板创建的,每个子视图都有一个按钮,点击按钮会带来另一个子视图并隐藏当前的子视图。 但是我发现当子视图调用removeFromSuperview()时,它会自动释放,如果我以后要使用这个子视图,我需要一个var来指向它。 这是我的代码:

class ViewController: UIViewController {

    @IBOutlet weak var secondView: UIView!
    @IBOutlet weak var firstView: UIView!
    var temp1: UIView?
    var temp2: UIView?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        temp1 = firstView
        temp2 = secondView
        secondView.removeFromSuperview()
    }
    @IBAction func moveToSecond(_ sender: UIButton) {
        firstView.removeFromSuperview()
        view.insertSubview(secondView, at: 0)
        secondView.isUserInteractionEnabled = true
    }
    @IBAction func moveToFirst(_ sender: UIButton) {
        secondView.removeFromSuperview()
        view.insertSubview(firstView, at: 0)
        firstView.isUserInteractionEnabled = true
    }
}

如果没有两个temp var,subview会在removeFromSuperview后释放,导致下一次insertSubview崩溃,因为它是nil。 那么,如何防止子视图自动释放? 是否有另一种方法可以在 StoryBoard graceful 创建的两个子视图之间切换?

我的错误,我没有注意到outlet是弱的,一切都是合理的,removeFromSuperview时,子视图没有强点,自动释放。

您可以根据您的要求使用 @IBOutlet var secondView: UIView! 创建 view.But 的强引用我建议不要将其从超级视图中删除。相反,您应该在需要时隐藏和显示视图,如下所示。

class ViewController: UIViewController {

@IBOutlet var secondView: UIView!
@IBOutlet var firstView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    secondView.isHidden = true
}
@IBAction func moveToSecond(_ sender: UIButton) {
    firstView.isHidden = true
    secondView.isHidden = false
    secondView.isUserInteractionEnabled = true
}
@IBAction func moveToFirst(_ sender: UIButton) {
    secondView.isHidden = true
    firstView.isHidden = false
    firstView.isUserInteractionEnabled = true
}
}

假设您的两个视图在上面一个,具有相同的宽度和相同的高度。在 viewDidLoad 中。首先将 firstView 设置为前面,将 secondView 设置为后面。单击按钮后,我将 secondView 置于前面,将 firstView 置于后面,依此类推。

import UIKit

class ViewController1: UIViewController {

    @IBOutlet var secondView: UIView!
    @IBOutlet var firstView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.bringSubview(toFront: firstView)
        self.view.sendSubview(toBack: secondView)
    }
    @IBAction func moveToSecond(_ sender: UIButton) {
        self.view.bringSubview(toFront: secondView)
        self.view.sendSubview(toBack: firstView)
    }
    @IBAction func moveToFirst(_ sender: UIButton) {
        self.view.bringSubview(toFront: firstView)
        self.view.sendSubview(toBack: secondView)
    }
}

如果您的两个子视图位于视图的不同位置。那么你需要在相应的按钮中隐藏替代视图点击

      class ViewController: UIViewController {

        @IBOutlet var secondView: UIView!
        @IBOutlet var firstView: UIView!
        override func viewDidLoad() {
            super.viewDidLoad()
            secondView.isHidden = true
            firstView.isUserInteractionEnabled = true
        }
        @IBAction func moveToSecond(_ sender: UIButton) {
            firstView.isHidden = true
            secondView.isHidden = false
            secondView.isUserInteractionEnabled = true
        }
        @IBAction func moveToFirst(_ sender: UIButton) {
            secondView.isHidden = true
            firstView.isHidden = false
            firstView.isUserInteractionEnabled = true
        }

 }