隐藏和显示(子视图)按钮或更改标题?

时间:2017-05-25 11:50:01

标签: ios iphone swift swift3

我的子视图存在问题。我有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
}

这里我添加了这个Subviews(这个func还添加了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方法,但它没有帮助。我也尝试在按钮名称之前使用self,这是隐藏但我也无法工作。有什么建议吗?

4 个答案:

答案 0 :(得分:0)

计算属性startButton,stopButton,resetButton不是作为子视图添加的SAME项。计算属性不存储值,它们为您提供获取值和设置更改的方法。因此,您在其中执行的操作isHidden不适用于您的视图。

答案 1 :(得分:0)

isHidden属性不能正常工作的原因是,当您的计算属性变量获得UIButton的新实例而不是旧实例时。如果您想要使用计算属性,那么您应该创建延迟属性。因此,无论何时调用属性变量,您都会得到一个UIButton的实例。

答案 2 :(得分:0)

您的属性始终会创建新按钮,因此您的引用将不同。设置isHidden时,将重新创建按钮,以便设置另一个未添加到bottomView的按钮的isHidden属性。

只需使用普通属性:

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

答案 3 :(得分:0)

您可以使用带有lazy 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
    }()