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

时间:2017-04-17 04:28:11

标签: ios storyboard subview

我希望更改两个子视图作为按钮被点击,子视图是由StoryBoard创建的,每个子视图都有一个按钮,单击按钮将带来另一个子视图和隐藏的当前子视图。 但是我发现当子视图调用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创建的两个子视图之间进行更改吗?

我的错误,我没注意到插座是弱的,一切都是合理的,当removeFromSuperview,子视图没有留下强点而不是自动释放。

2 个答案:

答案 0 :(得分:1)

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

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
}
}

答案 1 :(得分:0)

假设您的两个视图是上面的一个,具有相同的宽度和相同的高度。在viewDidLoad中。首先将firstView设置为front,将secondView设置为back。单击按钮后,我将第二个视图带到前面,将第一个视图带到后面,就像这样。

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
        }

 }