【问题标题】:How to handle custom radio buttons如何处理自定义单选按钮
【发布时间】:2019-01-31 09:37:43
【问题描述】:

您好,我需要在我的项目中添加单选按钮,例如 Radio Button Image 我将图像放入按钮中,选择展位并清空。 我该如何处理这些被选中的另一个是不选中的。 我们的选择只有一次。

@IBOutlet var nothisonlybtn: UIButton!
@IBOutlet var biweeklybtn: UIButton!
@IBOutlet var weeklybtn: UIButton!

我们如何使用按钮插座来处理

【问题讨论】:

标签: ios swift button radio-button


【解决方案1】:

您发布的图片显示了在这种情况下,表格视图会很好。

如果是这样,请使用 tableView tableView: didSelectItemAt indexPath: & tableView tableView: didDeselectItemAt indexPath: 方法。

func tableView(_ tableView: UITableView, didSelectItemAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! <#UITableViewCellClass#>
    cell.rImageBtn.setImage(<#selected image#>, for: .normal)
}

func tableView(_ tableView: UITableView, didDeselectItemAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! <#UITableViewCellClass#>
    cell.rImageBtn.setImage(<#unselected image#>, for: .normal)
}

如果图像是 UIView 中的单个实体,则:

@IBOutlet weak var rImageBtn1: UIButton!
@IBOutlet weak var rImageBtn2: UIButton!
@IBOutlet weak var rImageBtn3: UIButton!

@objc func button1Tapped() {
    rImageBtn1.setImage(<#selected image#>, for: .normal)
    rImageBtn2.setImage(<#unselected image#>, for: .normal)
    rImageBtn3.setImage(<#unselected image#>, for: .normal)
}

@objc func button2Tapped() {
    rImageBtn1.setImage(<#unselected image#>, for: .normal)
    rImageBtn2.setImage(<#selected image#>, for: .normal)
    rImageBtn3.setImage(<#unselected image#>, for: .normal)
}

@objc func button3Tapped() {
    rImageBtn1.setImage(<#unselected image#>, for: .normal)
    rImageBtn2.setImage(<#unselected image#>, for: .normal)
    rImageBtn3.setImage(<#selected image#>, for: .normal)
}

希望这能解决您的问题。

【讨论】:

  • 不,它不起作用我不使用表格视图,我将它们作为视图中的按钮。我们选择一个按钮自动选择按钮图像也改变了无需再次选择DeselectItem。
  • 如果您尝试第二个 sn-p,我希望它会起作用。它不需要表格视图,也不需要选择两次。选择一个按钮后,其他按钮将自动取消选择。
  • 对不起,我们不需要为每个按钮编写超级操作,我们将用单个操作编写那个。一旦观察我的代码波纹管
【解决方案2】:

我们为所有按钮提供了一种通用操作,并为这些标签提供了 0、1、2 等标签。

试试这个代码。

@IBAction func radioButtonAction(_ sender: UIButton) {

        weeklybtn.setImage(UIImage(named: "Radio Button"), for: .normal)
        biweeklybtn.setImage(UIImage(named: "Radio Button"), for: .normal)
        nothisonlybtn.setImage(UIImage(named: "Radio Button"), for: .normal)
        weeklylbl.textColor = UIColor(red: 92/255, green: 112/255, blue: 139/255, alpha: 1.0)
        biweeklylbl.textColor = UIColor(red: 92/225, green: 112/255, blue: 139/255, alpha: 1.0)
        onlyForthisLbl.textColor = UIColor(red: 92/225, green: 112/255, blue: 139/255, alpha: 1.0)
        if sender.tag == 0
        {
            weeklybtn.setImage(UIImage(named: "Checked"), for: .normal)
            weeklylbl.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
        }
        else if sender.tag == 1
        {
            biweeklybtn.setImage(UIImage(named: "Checked"), for: .normal)
            biweeklylbl.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
        }
        else
        {
            nothisonlybtn.setImage(UIImage(named: "Checked"), for: .normal)
            onlyForthisLbl.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
        }

    }

【讨论】:

    【解决方案3】:

    为所有按钮指定一个通用的IBOutletIBAction,并指定这些标签,例如 0、1、2。

    通用 IBOutlet

    @IBOutlet var radioButtons: [UIButton]!
    

    常见的 IBAction

    @IBAction func radioButtonTapped(_ sender: UIButton) {
    
       for button in radioButtons {
            if button.tag == sender.tag {
                button.setImage(UIImage(named: "Checked"), for: .normal)
                button.titleLabel!.textColor = UIColor(red: 46/225, green: 125/255, blue: 225/255, alpha: 1.0)
            }else{
                button.setImage(UIImage(named: "Radio Button"), for: .normal)
                button.titleLabel!.textColor = UIColor(red: 92/255, green: 112/255, blue: 139/255, alpha: 1.0)
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2021-04-23
      相关资源
      最近更新 更多