2017-04-17 79 views
1

我想改變兩個子視圖作爲按鈕被點擊,子視圖是由StoryBoard創建的,每個子視圖都有一個按鈕,點擊按鈕會帶來另一個子視圖並隱藏當前視圖。 但是我發現當子視圖調用removeFromSuperview()時,它會自動發佈,如果我想在以後使用這個子視圖,我需要一個var來指向它。 這裏是我的代碼:如何在調用removeFromSuperview後防止子視圖自我釋放?

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

沒有兩種溫度變種,子視圖將removeFromSuperview後釋放,並導致未來insertSubview崩潰,因爲它是零。 那麼,如何防止子視圖自動釋放? 有沒有另外一種方法來改變StoryBoard優雅創建的兩個子視圖?

我的錯誤,我沒有注意到插座是弱的,一切都合理,removeFromSuperview時,沒有強點留給子視圖,比它自動釋放。

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

謝謝你,我明白了。 –

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 
     } 

} 
+0

雖然您的答案顯示了一種解決方法,但它並不直接回答問題。 –

+0

在這個問題中沒有適當提及,如何將觀點放在主視圖上。所以根據兩種情況我寫了答案。謝謝 –

相關問題