【问题标题】:Single selection radioButton单选单选按钮
【发布时间】:2019-01-05 00:48:15
【问题描述】:

所以我有 3 个按钮

override func viewDidLoad() {
    super.viewDidLoad()

    weeklyRadioButton.setImage(UIImage(named: "a"), for: .normal)
    weeklyRadioButton.setImage(UIImage(named: "b"), for: .selected)

    biWeeklyRadioButton.setImage(UIImage(named: "a"), for: .normal)
    biWeeklyRadioButton.setImage(UIImage(named: "b"), for: .selected)

    noThanksRadioButton.setImage(UIImage(named: "a"), for: .normal)
    noThanksRadioButton.setImage(UIImage(named: "b"), for: .selected)
}

@IBAction func biWeeklyRadioButtonTapped(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
    weeklyRadioButton.setImage(UIImage(named: "a"), for: .normal) . 
    // its not changing then image of the button.
    noThanksRadioButton.setImage(UIImage(named: "a"), for: .normal)
}

以及其他两个按钮的类似两个操作。

当前代码允许多选按钮。 我想知道在按钮中实现单选的方法。

假设我选择了按钮 1 在选择按钮 2 时,按钮 1 被取消选择

【问题讨论】:

  • 是否可以添加额外的代码

标签: ios swift radio-button


【解决方案1】:

试试下面的代码。另请注意,所有按钮插座都应连接到以下操作方法。

@IBAction func anyRadioButtonTapped(_ sender: UIButton) {

    //Reset all of them first
    weeklyRadioButton.isSelected = false
    biWeeklyRadioButton.isSelected = false
    noThanksRadioButton.isSelected = false

    //Highlight associate button
    sender.isSelected = !sender.isSelected
}

【讨论】:

  • 不是与问题相关的有效答案
  • @Anbu.Karthik,我已经编辑了我的帖子。请检查一次,让我知道我错过了什么。谢谢...!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 2018-12-14
相关资源
最近更新 更多