gpt4 book ai didi

ios - Swift 单选按钮不可点击

转载 作者:行者123 更新时间:2023-11-28 15:37:27 31 4
gpt4 key购买 nike

我正在通过 Cocoa Pods 使用 DLRadioButton 库,发现 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")
}
}

`

最佳答案

你需要选择按钮的方法

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")
}
}

关于ios - Swift 单选按钮不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44147621/

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