gpt4 book ai didi

ios - 隐藏和显示( subview )按钮或更改标题?

转载 作者:行者123 更新时间:2023-11-28 08:09:10 24 4
gpt4 key购买 nike

我的 subview 有问题。我有 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
}

我在这里添加了这个 subview (这个函数还添加了 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,但我也不起作用。有什么建议吗?

最佳答案

您可以使用带有惰性键盘的计算属性创建按钮

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

关于ios - 隐藏和显示( subview )按钮或更改标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44179869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com