UIButton 导致无法识别的选择器发送到实例

UIButton causing unrecognized selector sent to instance

我正在尝试使用 for 循环创建多个按钮,但在使用 (sender:) 函数时遇到问题。

我有以下代码

func setUpButtons(){        
    for i in 1...3 {

    let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 100, width: 75, height: 100))
        btn.center = CGPoint(x: 20 + 100.0 * CGFloat(i), y: 200)
        btn.backgroundColor = UIColor.green
        btn.setTitle("Click Me", for: UIControlState.normal)
        btn.addTarget(self, action: Selector(("buttonAction:")), for: UIControlEvents.touchUpInside)
        btn.tag = i
        self.view.addSubview(btn)
    }
}
func buttonAction(sender: UIButton!) {
    let btnsendtag: UIButton = sender
    if btnsendtag.tag == 1 {
        print("Button 1")
    } else if btnsendtag.tag == 2 {
        print("Button 2")
    } else if btnsendtag.tag == 3 {
        print("Button 3")
    }
}

当我按下按钮时,它显示以下错误:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Learn_ABCs.ViewController buttonAction:]: unrecognized selector sent to instance 0x7f9789d0bc50'
    *** First throw call stack:
(
0   CoreFoundation                      0x00000001095ac34b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x00000001061fd21e objc_exception_throw + 48
2   CoreFoundation                      0x000000010961bf34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x0000000109531c15 ___forwarding___ + 1013
4   CoreFoundation                      0x0000000109531798 _CF_forwarding_prep_0 + 120
5   UIKit                               0x0000000106ddeb88 -[UIApplication sendAction:to:from:forEvent:] + 83
6   UIKit                               0x0000000106f642b2 -[UIControl sendAction:to:forEvent:] + 67
7   UIKit                               0x0000000106f645cb -[UIControl _sendActionsForEvents:withEvent:] + 444
8   UIKit                               0x0000000106f634c7 -[UIControl touchesEnded:withEvent:] + 668
9   UIKit                               0x0000000106e4c0d5 -[UIWindow _sendTouchesForEvent:] + 2747
10  UIKit                               0x0000000106e4d7c3 -[UIWindow sendEvent:] + 4011
11  UIKit                               0x0000000106dfaa33 -[UIApplication sendEvent:] + 371
12  UIKit                               0x00000001075ecb6d __dispatchPreprocessedEventFromEventQueue + 3248
13  UIKit                               0x00000001075e5817 __handleEventQueue + 4879
14  CoreFoundation                      0x0000000109551311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15  CoreFoundation                      0x000000010953659c __CFRunLoopDoSources0 + 556
16  CoreFoundation                      0x0000000109535a86 __CFRunLoopRun + 918
17  CoreFoundation                      0x0000000109535494 CFRunLoopRunSpecific + 420
18  GraphicsServices                    0x000000010c383a6f GSEventRunModal + 161
19  UIKit                               0x0000000106ddcf34 UIApplicationMain + 159
20  Learn ABCs                          0x0000000105c1fa7f main + 111
21  libdyld.dylib                       0x000000010a4d668d start + 1)
libc++abi.dylib: terminating with uncaught exception of type NSException

我觉得我的 : 是正确的,所以我不确定还要寻找什么。我正在使用 swift 3.0.

您的 Selector(("buttonAction:")) 可能是您遇到问题的原因。

试试 #selector(buttonAction(sender:))

使用这个

 func setUpButtons(){
        for i in 1...2 {


            let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 100, width: 75, height: 100))
            btn.center = CGPoint(x: 20 + 100.0 * CGFloat(i), y: 200)
            btn.backgroundColor = UIColor.greenColor()
            btn.setTitle("click", forState: UIControlState.Normal)

            btn.addTarget(self, action:#selector(buttonAction), forControlEvents: .TouchUpInside)
            self.view.addSubview(btn)
        }
    }

对于 Swift 4 -

    override func awakeFromNib() {
        button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
    }

    @objc func buttonClicked(sender:UIButton) {
        ...
    }

对于 iOS Facebook SDK 按钮,

将按钮 class 从 UIButton 分配给 FBSDKLoginButton,然后 运行 应用程序

它在设置委托、按钮权限时有效。

我想要这样的圆锥体

此代码使应用程序崩溃并出现相同的错误

@IBOutlet weak var loginAppleButton: ASAuthorizationAppleIDButton!

loginAppleButton.cornerRadius = 10

这修复了它

loginAppleButton.circle()

extension ASAuthorizationAppleIDButton {

func circle() {
    self.layer.cornerRadius = self.frame.height / 2
    }
}