ios - 无法创建程序化广播组

标签 ios swift swift3 radio-button

我正在尝试基于 Firebase 中的变量创建一个动态 radio 组。因此,Radio Group 中的按钮数量将取决于 Firebase 中的值。我正在使用 DLRadioButton pod 文件。

这是我的代码: 导入 UIKit 导入 Firebase 数据库 导入 DLRadioButton

class PollController: UIViewController {

@IBOutlet weak var passLabel: UILabel!
@IBOutlet weak var pollImage: UIImageView!

var ref: FIRDatabaseReference!
var pollRef: FIRDatabaseReference!

var pass = ""
var passedImageURL = ""

var posX = 0;
var posY = 0;

var numberOfChildren: Int = 0;

let label2 = UILabel(frame: CGRect(x: 90, y: 160, width: 200, height: 70))

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..<5 {
        let firstRadioButton = self.createRadioButton(frame: CGRect(x: CGFloat(x)*32, y: self.view.center.y, width: 40.0, height: 20.0), title: String(x), color: UIColor.green)
        firstRadioButton.tag = x
        buttons.append(firstRadioButton)
        self.view.addSubview(firstRadioButton);
    }

    let groupButtons = DLRadioButton()
    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.contentVerticalAlignment = UIControlContentVerticalAlignment.center;
    radioButton.addTarget(self, action: #selector(self.logSelectedButton(_:)), for: UIControlEvents.touchUpInside);
    return radioButton;
}

@objc private func logSelectedButton(_ sender: DLRadioButton){

    print("Selected Button Tag = \(sender.tag) and Title \(sender.titleLabel?.text)")
     }

 }

enter image description here

最佳答案

createRadioButton 需要改为 self.createRadioButton(..

关于ios - 无法创建程序化广播组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44103080/

相关文章:

android - 如何在 React Native 中监听 App 的启动和关闭?

ios - 动态设置Scrollview的Contentsize不起作用

swift3 - POST请求错误: NSURLConnection HTTP load failed iOS 10

ios - 在闭包中使用可变参数时编译器崩溃

ios - 如何在没有 "MFMessageComposeViewController "的情况下以编程方式在 iPhone 上发送短信?

ios - 使用 AVAssetReader 读取多个轨道

ios - 首次登录应用后自动填充iOS12提示信息用户保存密码?

Swift:将函数分配给变量

ios - 关闭按钮在 Swift Playground 中不起作用

ios - 无法在 Swift 3 中使用闭包语法