0

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

我得到的异常如下:

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

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

4

2 回答 2

3

因此,在界面生成器中,您可能创建了一个附加到按钮的 IBAction。看起来它最初被命名为“showSomething”,而您将其重命名为“showMessage”。

要修复该错误,您可以将方法名称更改回“showSomething”,或者您必须进入界面构建器,选择您的按钮,然后检查插座连接(右上角的按钮是一个圆圈里面有一个箭头)。

您将看到旧插座连接到 showSomething,删除它并将您的按钮重新连接到新功能“showMessage”。

于 2015-09-30T18:54:12.790 回答
0

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

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

@IBAction func showMessage(sender: AnyObject) { let alertController = UIAlertController(title:"Success",message:"Awesome done",preferredStyle:UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.默认,处理程序:无))

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

}

于 2015-10-01T06:31:40.507 回答