我想在swift中连接单选按钮方法和选择器语句

时间:2018-03-20 13:34:24

标签: ios swift xcode radio-button

let button8:ISRadioButton = ISRadioButton(frame: CGRect(x: 140, y: 75, width: 70, height: 35))

    button8.addTarget(self, action:#selector(buttonClicked4), for: .touchUpInside)

    button8.setTitle("Female", for: .normal)
    button8.setTitleColor(UIColor.black, for: .normal)
    button8.titleLabel?.font =  UIFont.boldSystemFont(ofSize: 12)

// method for radio button

@objc func buttonClicked4(_ isRadioButton:ISRadioButton){

    if isRadioButton.multipleSelectionEnabled{
        for radioButton in isRadioButton.otherButtons! {
            print("%@ is selected.\n", radioButton.titleLabel!.text ?? "");
        }
    }else{
        print("%@ is selected.\n", isRadioButton.titleLabel!.text ?? "");
    }


}

我想将单选按钮方法与选择器语句连接,但是通过此代码方法不执行

1 个答案:

答案 0 :(得分:0)

在审核完代码然后自行测试后,我认为该问题与addTarget方法有关。您的功能包括_ isRadioButton: ISRadioButton,但是当您将其添加到目标时,您会使用buttonClicked4。如果你注意到,宣布之后没有任何内容。因此,在(_:)方法声明函数后,您需要包含addTarget

您的代码应该如下:

let button8: ISRadioButton = ISRadioButton(frame: CGRect(x: 140, y: 75, width: 70, height: 35))

// Changed buttonClicked4 to buttonClicked4(_:)
button8.addTarget(self, action: #selector(buttonClicked4(_:)), for: .touchUpInside)

button8.setTitle("Female", for: .normal)
button8.setTitleColor(UIColor.black, for: .normal)
button8.titleLabel?.font =  UIFont.boldSystemFont(ofSize: 12)

@objc func buttonClicked4(_ isRadioButton: ISRadioButton) {
    if isRadioButton.multipleSelectionEnabled {
        for radioButton in isRadioButton.otherButtons! {
            print("%@ is selected.\n", radioButton.titleLabel!.text ?? "")
        }
    } else {
        print("%@ is selected.\n", isRadioButton.titleLabel!.text ?? "")
    }
}