2017-05-24 4 views
0

Ich bin mit der DLRadioButton Bibliothek durch Cocoa Podshere gefundenSwift Radio-Buttons nicht anklickbar

Unter meinem Code ist im Wesentlichen der Radio-Buttons jedoch erscheinen ich nur in der Lage bin davon 1 in der Gruppe klicken, und ich bin nicht sicher, warum . Beachten Sie, dass ich die Radio-Buttons auf einem Int in Firebase basierend bin bevölkern, die angibt, wie viele bevölkern:

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

`

Antwort

1

Sie Methode der Schaltfläche zum Wähler müssen

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) 

Wahlverfahren : Sie können je nach Bedarf ändern

@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") 
     } 
    } 
+0

Danke für die Hilfe, aber ich bekomme den Fehler "Typ DLRadioButton entspricht nicht dem Protokolltyp" Sequenz. "Ich würde das Bild veröffentlichen, aber StackOverflow hat anscheinend Probleme mit Imgur. – tccpg288

+0

Wenn Protokoll in der Schnittstelle hinzugefügt: UIviewcontroller, sequenceDelegate ..... dann müssen Sie es erforderlichen Delegate-Methode in der Klasse hinzufügen. – KKRocks