2017-05-25 105 views
1

我的子視圖有問題。我有2個可見的按鈕和1,它應該顯示後點擊第一個。更準確地說,我有開始按鈕,重置按鈕和停止按鈕。加載時應該只顯示「開始」和「重置」按鈕,但是當我按下開始按鈕時,應該隱藏「重置」按鈕並顯示停止按鈕。像isHidden這樣的語法不起作用。問題是什麼?隱藏和顯示(子視圖)按鈕或更改標題?

星,停止和復位按鈕:

var stopButton: UIButton{ 

    let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100)) 
    stopButton.backgroundColor = .white 
    stopButton.setTitle("Stop", for: .normal) 
    stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside) 
    stopButton.layer.cornerRadius = 50 
    stopButton.layer.masksToBounds = true 
    stopButton.isHidden = true 

    return stopButton 
} 

var resetButton: UIButton{ 

    let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100)) 
    resetButton.backgroundColor = .red 
    resetButton.setTitle("Reset", for: .normal) 
    resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside) 
    resetButton.layer.cornerRadius = 50 
    resetButton.layer.masksToBounds = true 

    return resetButton 
} 

var startButton: UIButton{ 

    let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) 
    startButton.backgroundColor = .green 
    startButton.setTitle("Start", for: .normal) 
    startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside) 
    startButton.layer.cornerRadius = 50 
    startButton.layer.masksToBounds = true 

    return startButton 
} 

在這裏,我添加此子視圖(本功能也添加UIView的,這就是所謂的viewDidLoad中):

func addBottomView() 
{ 
    let orginX = (collectionView?.frame.minX)! 
    let orginY = (collectionView?.frame.maxY)! 
    let heightOfView = view.frame.height - (view.frame.height/100) * 70 

    let bottomView = UIView(frame: CGRect(x: orginX, y: orginY, width: view.frame.width, height: heightOfView)) 
    bottomView.backgroundColor = .orange 

    view.addSubview(bottomView) 

    bottomView.addSubview(stopButton) 
    bottomView.addSubview(startButton) 
    bottomView.addSubview(resetButton) 


} 

這裏是我的功能,這是連接到這個按鈕:

func startButtonAction() 
{ 
    blinkAction() 
    print("START ACTION") 

    resetButton.isHidden = true 
    stopButton.isHidden = false 
    view.layoutSubviews() 
} 

func resetButtonAction() 
{ 
    print("RESET ACTION") 
    blinkAction() 

} 
func stopButtonAction() 
{ 
    print("STOP ACTION") 
    blinkAction() 

    resetButton.isHidden = false 
    stopButton.isHidden = true 
    view.layoutSubviews() 

} 

我添加layoutSubviews方法,但它沒有幫助。我也嘗試使用隱藏的按鈕名稱之前,但我也不工作。有什麼建議?

+0

計算的特性創建按鈕,爲什麼你在STOPBUTTON計算財產使用「stopButton.isHidden =真」? –

+0

因爲在加載停止按鈕應該被隱藏。只有點擊開始按鈕後 –

回答

0

可以使用懶keywoard

lazy var stopButton: UIButton = { 
     let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100)) 
     stopButton.backgroundColor = .yellow 
     stopButton.setTitle("Stop", for: .normal) 
     stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside) 
     stopButton.layer.cornerRadius = 50 
     stopButton.isHidden = true 
     stopButton.layer.masksToBounds = true 
     return stopButton 
    }() 

    lazy var resetButton: UIButton = { 
     let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100)) 
     resetButton.backgroundColor = .red 
     resetButton.setTitle("Reset", for: .normal) 
     resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside) 
     resetButton.layer.cornerRadius = 50 
     resetButton.layer.masksToBounds = true 
     return resetButton 
    }() 

    lazy var startButton: UIButton = { 
     let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) 
     startButton.backgroundColor = .green 
     startButton.setTitle("Start", for: .normal) 
     startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside) 
     startButton.layer.cornerRadius = 50 
     startButton.layer.masksToBounds = true 
     return startButton 
    }() 
0

計算出的屬性startButton,stopButton,resetButton不是作爲子視圖添加的相同項目。計算屬性不存儲值,它們爲您提供獲取值和設置更改的方法。所以你採取的行動是隱藏的不適用於你的觀點。

0

isHidden屬性效果不好的原因是,當你的計算屬性變量你得到一個新的實例UIButton而不是舊的。如果你想要使用計算屬性,那麼你應該創建懶惰屬性。所以,無論何時你調用屬性變量,你都會得到一個UIButton的舊實例。

0

你的屬性總是創建新的按鈕,所以你的引用將不會相同。當您設置isHidden時,您的按鈕將被重新創建,以便設置未添加到bottomView的另一個按鈕的isHidden propery。

只需使用普通的屬性:

var stopButton: UIButton! 
var resetButton: UIButton! 
var startButton: UIButton! 

創建按鈕:

func createButtons() { 

let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100)) 
stopButton.backgroundColor = .white 
stopButton.setTitle("Stop", for: .normal) 
stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside) 
stopButton.layer.cornerRadius = 50 
stopButton.layer.masksToBounds = true 
stopButton.isHidden = true 

let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100)) 
resetButton.backgroundColor = .red 
resetButton.setTitle("Reset", for: .normal) 
resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside) 
resetButton.layer.cornerRadius = 50 
resetButton.layer.masksToBounds = true 

let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) 
startButton.backgroundColor = .green 
startButton.setTitle("Start", for: .normal) 
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside) 
startButton.layer.cornerRadius = 50 
startButton.layer.masksToBounds = true 
} 

添加爲子視圖:

bottomView.addSubview(stopButton) 
bottomView.addSubview(startButton) 
bottomView.addSubview(resetButton) 

,並利用它們:

resetButton.isHidden = false 
stopButton.isHidden = true