单击按钮时,Swift“无法识别的选择器发送到实例”

时间:2017-05-16 09:58:47

标签: ios iphone swift xcode

我正在尝试按下按钮点击工作,但我得到的只是下面的错误。

Heres是关键代码

 let button = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
                        button.center = CGPoint(x: 260, y: 220)

                        button.setTitle(NSLocalizedString("READ ONLINE", comment: "Button"), for: .normal)
                        button.backgroundColor = UIColor(red: 0.0, green: 0.600, blue: 0.800, alpha: 1.0)
                        button.addTarget(self, action: "buttonAction:", for: UIControlEvents.touchUpInside)
                        button.tag = 22;

                        func buttonAction(_sender:UIButton!)
                        {
                            var btnsendtag:UIButton = _sender
                            if btnsendtag.tag == 22 {
                                print("Button tapped tag 22")
                            }
                        }

这是我得到的错误。

unrecognized selector sent to instance 0x7fec8fc14950'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010b1b034b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010ac1121e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010b21ff34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010b135c15 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010b135798 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010b834b88 -[UIApplication sendAction:to:from:forEvent:] + 83
    6   UIKit                               0x000000010b9ba2b2 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x000000010b9ba5cb -[UIControl _sendActionsForEvents:withEvent:] + 444
    8   UIKit                               0x000000010b9b94c7 -[UIControl touchesEnded:withEvent:] + 668
    9   UIKit                               0x000000010b8a20d5 -[UIWindow _sendTouchesForEvent:] + 2747
    10  UIKit                               0x000000010b8a37c3 -[UIWindow sendEvent:] + 4011
    11  UIKit                               0x000000010b850a33 -[UIApplication sendEvent:] + 371
    12  UIKit                               0x000000010c042b6d __dispatchPreprocessedEventFromEventQueue + 3248
    13  UIKit                               0x000000010c03b817 __handleEventQueue + 4879
    14  CoreFoundation                      0x000000010b155311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x000000010b13a59c __CFRunLoopDoSources0 + 556
    16  CoreFoundation                      0x000000010b139a86 __CFRunLoopRun + 918
    17  CoreFoundation                      0x000000010b139494 CFRunLoopRunSpecific + 420
    18  GraphicsServices                    0x0000000110fe1a6f GSEventRunModal + 161
    19  UIKit                               0x000000010b832f34 UIApplicationMain + 159
    20  BebrightbookOffline                 0x000000010a6288df main + 111
    21  libdyld.dylib                       0x000000010e2ed68d start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

有什么想法吗?

我猜这个功能没有被正确引用,但我想出了原因。 (我对swift没有经验)

3 个答案:

答案 0 :(得分:4)

问题是您没有以正确的方式向按钮添加操作。请这样做

 button.addTarget(self, action: #selector(buttonAction), for: UIControlEvents.touchUpInside)

答案 1 :(得分:1)

按如下方式替换按钮操作分配:

button.addTarget(self, action: #selector(self.buttonAction(_sender:)), for: UIControlEvents.touchUpInside)

答案 2 :(得分:0)

试试这个,

  let button = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
    button.center = CGPoint(x: 260, y: 220)

    button.setTitle(NSLocalizedString("READ ONLINE", comment: "Button"), for: .normal)
    button.backgroundColor = UIColor(red: 0.0, green: 0.600, blue: 0.800, alpha: 1.0)
   // button.addTarget(self, action: Selector(("buttonAction:")), for: UIControlEvents.touchUpInside)

    button.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
    button.tag = 22;

和你的功能,

 func buttonAction(sender:UIButton)
{
    let btnsendtag:UIButton = sender
    if btnsendtag.tag == 22 {
        print("Button tapped tag 22")
    }
}