【问题标题】:Swift Radio Buttons Not ClickableSwift 单选按钮不可点击
【发布时间】:2017-05-25 09:36:58
【问题描述】:

我正在使用DLRadioButton 库,通过Cocoa Pods 找到here

下面是我的代码,基本上出现了单选按钮,但是我只能单击组中的其中一个,我不确定为什么。请注意,我正在根据Firebase 中的Int 填充单选按钮,这表明要填充多少:

`   override func viewDidLoad() {
    super.viewDidLoad()
    ref = FIRDatabase.database().reference()
    pollRef = ref.child("Polls").child(pass)
    passLabel.text = pass
    pollImage.sd_setImage(with: URL(string: passedImageURL), placeholderImage: UIImage(named: "test"))

    pollRef.observe(FIRDataEventType.value, with: {(snapshot) in
    self.numberOfChildren = Int(snapshot.childSnapshot(forPath: "answers").childrenCount)
    self.passLabel.text = String(self.numberOfChildren)
    print(self.numberOfChildren)

    var buttons = [DLRadioButton]()

    for x in 0..<self.numberOfChildren {
    let answerLabel = snapshot.childSnapshot(forPath: 
    "answers").childSnapshot(forPath: 
     String(x+1)).childSnapshot(forPath: "answer").value
    let firstRadioButton = self.createRadioButton(frame: CGRect(x: 
     CGFloat(x)*32, y:self.view.center.y , width: 40.0, height: 20.0), 
     title:  answerLabel as! String, color: UIColor.black)
    //        firstRadioButton.translatesAutore sizingMaskIntoConstraints = false
    let screenSize: CGRect = UIScreen.main.bounds
    firstRadioButton.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)
            firstRadioButton.tag = x
            buttons.append(firstRadioButton)
            self.view.addSubview(firstRadioButton);
        }


    let groupButtons = DLRadioButton()
    groupButtons.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(groupButtons)
    let margins = self.view.layoutMarginsGuide
    groupButtons.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: 0).isActive = true

    groupButtons.otherButtons = buttons
    })



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private func createRadioButton(frame : CGRect, title : String, color : UIColor) -> DLRadioButton {
    let radioButton = DLRadioButton(frame: frame);
    radioButton.titleLabel!.font = UIFont.systemFont(ofSize: 14);
    radioButton.setTitle(title, for: UIControlState.normal);
    radioButton.setTitleColor(color, for: UIControlState.normal);
    radioButton.iconColor = color;
    radioButton.indicatorColor = color;
    radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left;
    radioButton.addTarget(self, action: #selector(self.logSelectedButton(_:)), for: UIControlEvents.touchUpInside);
    return radioButton;
}

@IBAction func logSelectedButton(_ radioButton: DLRadioButton) {
    if radioButton.isMultipleSelectionEnabled {
        for button: DLRadioButton in radioButton.selected() {
            print("\(button.titleLabel?.text) is selected.\n")
        }
    }
    else {
        print("\(radioButton.selected()?.titleLabel?.text) is selected.\n")
    }
}

`

【问题讨论】:

    标签: ios swift swift3 radio-button radiobuttonlist


    【解决方案1】:

    你需要选择按钮的方法

    let firstRadioButton = self.createRadioButton(frame: CGRect(x: self.view.center.x, y: CGFloat(x)*32, width: 100.0, height: 120.0), title: answerLabel as! String, color: UIColor.green)
    firstRadioButton.tag = x
    firstRadioButton.addTarget(self, action: #selector(self.logSelectedButton), for: .touchUpInside)
    buttons.append(firstRadioButton)
    

    选择器方式:可根据需要更改

    @IBAction func logSelectedButton(_ radioButton: DLRadioButton) {
            if radioButton.isMultipleSelectionEnabled {
                for button: DLRadioButton in radioButton.selectedButtons {
                    print("\(button.titleLabel?.text) is selected.\n")
                }
            }
            else {
                print("\(radioButton.selectedButton.titleLabel?.text) is selected.\n")
            }
        }
    

    【讨论】:

    • 感谢您的帮助,但我收到错误消息“DLRadioButton 类型不符合协议类型“序列”。我会发布图片,但 StackOverflow 显然与 Imgur 存在问题。
    • 如果在接口中添加了协议:UIviewcontroller, sequeceDelegate.....那么你需要在类中添加它所需的委托方法。
    猜你喜欢
    • 2019-04-30
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多