单击 swift 中的按钮时出错

Getting error on clicking button in swift

我是 iOS 开发的新手,刚开始学习 swift 中的基础知识。现在我在尝试 运行 我正在使用自动布局练习的基本编码时遇到错误。当我尝试在单击按钮时打开对话框时发生以下异常。我也发布我的示例代码供您参考

我得到的异常如下:

<NSLayoutConstraint:0x79e84ca0 V:[UILabel:0x79e82b40'sadadsfsadfsd']-(90)-[_UILayoutGuide:0x79e84610]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-01 00:04:18.833 FirstDemo[6922:355986] -[FirstDemo.ViewController showSomething]: unrecognized selector sent to instance 0x79e80210
2015-10-01 00:04:18.836 FirstDemo[6922:355986] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstDemo.ViewController showSomething]: unrecognized selector sent to instance 0x79e80210'
*** First throw call stack:
(
    0   CoreFoundation                      0x0025aa94 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01e5ce02 objc_exception_throw + 50
    2   CoreFoundation                      0x00263de3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x001a1e3d ___forwarding___ + 1037
    4   CoreFoundation                      0x001a1a0e _CF_forwarding_prep_0 + 14
    5   libobjc.A.dylib                     0x01e710b5 -[NSObject performSelector:withObject:withObject:] + 84
    6   UIKit                               0x00a3dc40 -[UIApplication sendAction:to:from:forEvent:] + 118
    7   UIKit                               0x00a3dbbf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    8   UIKit                               0x00bd38fc -[UIControl sendAction:to:forEvent:] + 79
    9   UIKit                               0x00bd3c7c -[UIControl _sendActionsForEvents:withEvent:] + 408
    10  UIKit                               0x00bd2c87 -[UIControl touchesEnded:withEvent:] + 714
    11  UIKit                               0x00ab4eb8 -[UIWindow _sendTouchesForEvent:] + 1095
    12  UIKit                               0x00ab5da4 -[UIWindow sendEvent:] + 1118
    13  UIKit                               0x00a5e9b3 -[UIApplication sendEvent:] + 266
    14  UIKit                               0x00a35903 _UIApplicationHandleEventQueue + 7327
    15  CoreFoundation                      0x00174e7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    16  CoreFoundation                      0x0016ab0b __CFRunLoopDoSources0 + 523
    17  CoreFoundation                      0x00169f28 __CFRunLoopRun + 1032
    18  CoreFoundation                      0x00169866 CFRunLoopRunSpecific + 470
    19  CoreFoundation                      0x0016967b CFRunLoopRunInMode + 123
    20  GraphicsServices                    0x045fa664 GSEventRunModal + 192
    21  GraphicsServices                    0x045fa4a1 GSEventRun + 104
    22  UIKit                               0x00a3bcc1 UIApplicationMain + 160
    23  FirstDemo                           0x000784fc main + 140
    24  libdyld.dylib                       0x028cda21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

ViewController.swift

import UIKit

class ViewController: UIViewController {

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

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


    @IBAction func showMessage(){

        let alertController = UIAlertController(title:"Success",message:"Awesome done",preferredStyle:UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

请帮助我。如果我的问题不清楚,请给我发短信。提前致谢

因此,在界面生成器中,您可能创建了一个附加到按钮的 IBAction。它最初的名称似乎是 'showSomething',您将其重命名为 'showMessage'。

要修复该错误,您可以将方法名称改回 'showSomething',或者您必须进入界面生成器,select 您的按钮,并检查插座连接(右上角的按钮是一个带箭头的圆圈)。

您会看到您的旧插座连接显示一些东西,将其删除并将您的按钮重新连接到您的新功能 'showMessage'。

你给你的按钮连接了吗?如果不这样做

创建您的按钮 IBOutlet 并定义您的方法,否则像这样创建 IBAction

@IBAction func showMessage(发件人:AnyObject) { 让 alertController = UIAlertController(标题:"Success",消息:"Awesome done",preferredStyle:UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(标题:"Ok",样式:UIAlertActionStyle.Default,处理程序:nil))

    self.presentViewController(alertController, animated: true, completion: nil)

}