【问题标题】:Swift "unrecognized selector sent to instance" on button click [duplicate]按钮单击时迅速“发送到实例的无法识别的选择器”[重复]
【发布时间】:2017-05-16 09:58:46
【问题描述】:

我试图让一个按钮点击工作,但我得到的只是下面的错误。

这是关键代码

 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 的经验)

【问题讨论】:

  • 你在哪里添加按钮?
  • @Tiago Roque - 根据以下答案替换您的按钮操作并尝试。您的问题将得到解决。

标签: ios iphone swift xcode


【解决方案1】:

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

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

【讨论】:

  • 终于成功了。添加以将 buttonAction 更改为 self.buttonAction
【解决方案2】:

替换按钮动作分配如下:

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

【讨论】:

    【解决方案3】:

    这样试试,

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

    【讨论】:

      猜你喜欢
      • 2018-07-20
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多