2017-05-24 1 views
0

내가 Cocoa Pods 아래는스위프트 라디오 버튼

here은 본질적으로 라디오 버튼 그러나 나는 그룹에서 그 중 하나를 클릭 할 경우에만 수 있어요 내가 왜 확실하지 오전 표시, 내 코드 발견을 통해 DLRadioButton 라이브러리를 사용하고 있습니다 .

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

`

답변

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) 

선택기 방법의 방법을 선택해야합니다 내가 채우기 위해 얼마나 많은 나타냅니다 FirebaseInt에 따라 라디오 버튼을 채우는하고 있습니다 : 필요에 따라 변경할 수 있습니다.

@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

도움을 주셔서 감사합니다. 그러나 "DLRadioButton이 프로토콜 유형"시퀀스에 맞지 않습니다. "라는 오류가 나타납니다. 이미지를 게시하지만 StackOverflow에 Imgur와 관련된 문제가있는 것 같습니다. – tccpg288

+0

UIviewcontroller, sequeceDelegate ..... 인터페이스에 프로토콜을 추가 한 경우 클래스에 필수 위임 메서드를 추가해야합니다. – KKRocks