gpt4 book ai didi

ios - 按下时隐藏 UIButton,然后按下另一个按钮时显示它

转载 作者:行者123 更新时间:2023-11-30 12:21:32 26 4
gpt4 key购买 nike

我有三个按钮可以更改背景颜色。我想在按下红色按钮时隐藏它。如果按下绿色或蓝色按钮,则会显示它。绿色和蓝色按钮也是如此。

我找不到调用removeFromSuperview的方法。在 ObjectiveC 中我曾经这样做 mybutton.hidden = true但这不起作用。

ViewController: UIViewController {
@IBAction func RED(_ sender: Any) {

print("background was \(String(describing: self.view.backgroundColor))")
self.view.backgroundColor = UIColor.red
print("background is now \(String(describing: self.view.backgroundColor))")
}

@IBAction func GREEN(_ sender: Any) {
print("background was \(String(describing: self.view.backgroundColor))")
self.view.backgroundColor = UIColor.green
print("background is now \(String(describing: self.view.backgroundColor))")
}

@IBAction func BLUE(_ sender: UIButton) {
print("background was \(String(describing: self.view.backgroundColor))")
self.view.backgroundColor = UIColor.blue
print("background is now \(String(describing: self.view.backgroundColor))")
}

最佳答案

您需要为每个按钮创建一个 IBOutlet 才能访问“isHidden”属性。现在您只为每个定义了一个 IBAction (至少在您提供的代码中)。要创建 IBOutlet,请按住 Control 键并从按钮拖动到 View Controller 的代码,类似于创建已有的 IBActions 的操作。在弹出窗口中,确保“连接”为“ socket ”,“类型”为“UIButton”。根据需要命名它们(例如,redButton、blueButton)。然后您可以在适当的位置输入“redButton.isHidden = true”。

IBAction 只允许您控制按下按钮时将执行的操作。需要 IBOutlet 来访问 UIButton 的属性。

关于ios - 按下时隐藏 UIButton,然后按下另一个按钮时显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737578/

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