UIButton导致无法识别的选择器发送到实例 [英] UIButton causing unrecognized selector sent to instance

查看:171
本文介绍了UIButton导致无法识别的选择器发送到实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create multiple buttons with a for loop, but I'm having problems using the (sender:) function.

我有以下代码

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

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

When I press a button it shows the following error:

    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.

I feel like I have the : correct, so I'm not sure what else to look for. I'm using swift 3.0.

推荐答案

您的Selector(("buttonAction:"))可能是造成问题的原因.

Your Selector(("buttonAction:")) might be the cause of your problem.

改为尝试#selector(buttonAction(sender:)).

这篇关于UIButton导致无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆