点击swift

时间:2015-09-30 18:43:43

标签: ios swift autolayout uialertcontroller uialertaction

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

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

2 个答案:

答案 0 :(得分:2)

因此,在界面构建器中,您可能创建了一个附加到按钮的IBAction。看起来它原本被命名为“showSomething&#39;并将其重命名为&#39; showMessage&#39;。

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

您将看到旧的插座连接showSomething,将其删除,然后将按钮重新连接到新功能&#39; showMessage&#39;。

答案 1 :(得分:0)

您是否已将按钮连接到按钮?如果不这样做

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

@IBAction func showMessage(sender:AnyObject) { let alertController = UIAlertController(标题:“成功”,消息:“Awesome done”,preferredStyle:UIAlertControllerStyle.Alert)         alertController.addAction(UIAlertAction(标题:“Ok”,样式:UIAlertActionStyle.Default,handler:nil))

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

}