在 Xcode 8/Swift 3 中按下 UIButton 时出现“无法识别的选择器发送到实例”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45395603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:09:37  来源:igfitidea点击:

"Unrecognized selector sent to instance" error when pressing UIButton in Xcode 8/Swift 3

iosswiftxcode

提问by Will Titus

I am making an app that requires a user to press one of two UIButtonsand then prints a response depending on which button was pressed (determined based on the tag of the button pressed). However, when I run press button, I get an

我正在制作一个应用程序,要求用户按下两个按钮之一,UIButtons然后根据按下的按钮(根据按下的按钮的标签确定)打印响应。但是,当我运行按下按钮时,我得到一个

Unrecognized selector sent to instance

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

How do I resolve this?

我该如何解决?

Here's my code. The IBActionat the bottom refers to the UIButtonsthat are causing the error:

这是我的代码。该IBAction底部是指UIButtons那些会导致错误:

import UIKit

class ViewController: UIViewController {

    let story1 = "This is the story."
    let answer1 = "No change"
    let answer2 = "Lose 1 health"
    var healthStat : Int = 10


    @IBOutlet weak var storyText: UILabel!
    @IBOutlet weak var health: UILabel!
    @IBOutlet weak var restartButton: UILabel!
    @IBOutlet weak var choiceOne: UIButton!
    @IBOutlet weak var choiceTwo: UIButton!

    var storyIndex = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        storyText.text = story1
        choiceOne.setTitle("\(answer1)", for: .normal)
        choiceTwo.setTitle("\(answer2)", for: .normal)
        health.text = String("Health: \(healthStat)")

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPressed(_ sender: UIButton) {        
        if sender.tag == 1 {
            print("Option 1 selected")
        }
        else if sender.tag == 2 {
            print("Option 2 selected. Lose 1 health.")
            healthStat = healthStat - 1
        }

    }



}

Here is the entirety of the error:

这是错误的全部内容:

2017-07-29 21:45:24.626 Choose Your Own Adventure[16001:694686] -[Choose_Your_Own_Adventure.ViewController buttonOnePressed:]: unrecognized selector sent to instance 0x7fc199c0cfe0
2017-07-29 21:45:24.638 Choose Your Own Adventure[16001:694686] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Choose_Your_Own_Adventure.ViewController buttonOnePressed:]: unrecognized selector sent to instance 0x7fc199c0cfe0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000113504b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000011037e141 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000113574134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000011348b840 ___forwarding___ + 1024
    4   CoreFoundation                      0x000000011348b3b8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000110c66d82 -[UIApplication sendAction:to:from:forEvent:] + 83
    6   UIKit                               0x0000000110deb5ac -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x0000000110deb8c7 -[UIControl _sendActionsForEvents:withEvent:] + 450
    8   UIKit                               0x0000000110dea802 -[UIControl touchesEnded:withEvent:] + 618
    9   UIKit                               0x0000000110cd47ea -[UIWindow _sendTouchesForEvent:] + 2707
    10  UIKit                               0x0000000110cd5f00 -[UIWindow sendEvent:] + 4114
    11  UIKit                               0x0000000110c82a84 -[UIApplication sendEvent:] + 352
    12  UIKit                               0x00000001114665d4 __dispatchPreprocessedEventFromEventQueue + 2926
    13  UIKit                               0x000000011145e532 __handleEventQueue + 1122
    14  CoreFoundation                      0x00000001134aac01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x00000001134900cf __CFRunLoopDoSources0 + 527
    16  CoreFoundation                      0x000000011348f5ff __CFRunLoopRun + 911
    17  CoreFoundation                      0x000000011348f016 CFRunLoopRunSpecific + 406
    18  GraphicsServices                    0x000000011542da24 GSEventRunModal + 62
    19  UIKit                               0x0000000110c65134 UIApplicationMain + 159
    20  Choose Your Own Adventure           0x000000010fd9efa7 main + 55
    21  libdyld.dylib                       0x00000001144a465d start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

回答by Hussain Shabbir

It seems like you have rename your IBActionmethod name earlier it was different and it is connected to the previous name inside your storyboard. So just disconnect your action method and reconnect it appropriately.

看起来您之前已经重命名了您的IBAction方法名称,但它是不同的,并且它与故事板中的先前名称相关联。因此,只需断开您的操作方法并适当地重新连接即可。