如果未单击 button1,则禁用 button2
Posted
技术标签:
【中文标题】如果未单击 button1,则禁用 button2【英文标题】:Disable button2 if button1 is not clicked 【发布时间】:2019-05-22 05:38:27 【问题描述】:如果没有单击 button1,我想禁用 button2,并在单击 button1 时启用它。问题是,即使我已经点击了 button1,button2 也被禁用。我没有收到任何错误,我在 Xcode10 中使用 Swift4。
这是我的代码:
class ViewController: UIViewController
@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
override func viewDidLoad()
super.viewDidLoad()
intro()
btn1.setImage(UIImage(named: "checked.png"), for: .selected)
btn1.setImage(UIImage(named: "unchecked.png"), for: .normal)
@IBAction func button1(_ sender: UIButton)
UIView.animate(withDuration: 0.5, delay: 0.1, options: .curveLinear, animations:
sender.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
) (success) in
UIView.animate(withDuration: 0.5, delay: 0.1, options: .curveLinear, animations:
sender.isSelected = !sender.isSelected
sender.transform = .identity
, completion: nil)
func intro()
if btn1.isSelected == true
btn2.isUserInteractionEnabled = true
else
btn2.isUserInteractionEnabled = false
我也试过这段代码:
func intro()
if btn1.isTouchInside == true
btn2.isUserInteractionEnabled = true
else
btn2.isUserInteractionEnabled = false
【问题讨论】:
请完成您的问题 【参考方案1】:我认为首先你希望 button2 被禁用,当你点击 button1 时它应该被启用。
为此制作
override func viewDidLoad()
super.viewDidLoad()
btn1.setImage(UIImage(named: "checked.png"), for: .selected)
btn1.setImage(UIImage(named: "unchecked.png"), for: .normal)
btn2.isenable = false
@IBAction func button1(_ sender: UIButton)
if sender.isSelected == true
btn2.isenable = true
else
btn2.isenable = false
【讨论】:
【参考方案2】:在您的viewDidLoad()
中添加以下任何代码
btn2.isEnabled = false
或
btn2.isUserInteractionEnabled = false
并将您的按钮操作更改为,
@IBAction func button1(_ sender: UIButton)
if btn1.isSelected == true
btn2.isUserInteractionEnabled = true
else
btn2.isUserInteractionEnabled = false
【讨论】:
以上是关于如果未单击 button1,则禁用 button2的主要内容,如果未能解决你的问题,请参考以下文章
ExtJS button2 onClick 按组件执行click button1