2017-06-09 37 views
0

enter image description here從viewController快速更換容器

在主viewController中,我放置了容器視圖和2個按鈕下面。我想通過這些按鈕只更改容器部分。默認顯示的是容器1,按下按鈕2時,容器部分切換到容器2。我應該怎麼做?謝謝,

回答

0

讓IBOutlet中與這個故事板相關的您的視圖控制器,方法時按下按鈕叫 - button1Pressed和button2Pressed - > `

func button1Pressed() { 
    yourContainer1.isHidden = true 
    yourContainer2.isHidden = false 
} 

func button2Pressed() { 
    yourContainer1.isHidden = false 
    yourContainer2.isHidden = true 
} 

`

+0

我厭倦了你的代碼在許多方面,但沒有奏效。 – sandalwalk

+0

我想,那是因爲你沒有把你的IBActions和我的功能聯繫起來,因爲你使用了故事板。所以,你在答案中做了同樣的事情,但是用阿爾法。假設你已經爲你的視圖着色了,你需要改變它的alpha值,你打算怎麼做?正確的,使用isHidden屬性。 – Bliss

+0

是的,我無法將我的IBAction與您的功能相關聯。你能告訴我怎麼做嗎? – sandalwalk

0

基於this文章中,我做了我版本和它的作品。 enter image description here

在他的文章中,他將接口構建器中的container2的alpha值設置爲0。但是,這並不適合我。因此,我在界面生成器1中爲這兩個容器設置了alpha值,如下面的截圖所示。 enter image description hereenter image description here

這裏是main viewController的代碼。

import UIKit 

class ViewController: UIViewController { 


    @IBOutlet weak var containerViewA: UIView! 

    @IBOutlet weak var containerViewB: UIView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // below sets the container1 as default 
     self.containerViewA.alpha = 1 
     self.containerViewB.alpha = 0 

    } 


    @IBAction func button1(_ sender: Any) { 

     self.containerViewA.alpha = 1 
     self.containerViewB.alpha = 0 

    } 

    @IBAction func button2(_ sender: Any) { 

     self.containerViewA.alpha = 0 
     self.containerViewB.alpha = 1 

    } 

}