swift - ios自定义按钮报 NSInvalidArgumentException 异常

查看:140
本文介绍了swift - ios自定义按钮报 NSInvalidArgumentException 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

使用的 Xcode 是8.0的,不知有没什么不同

跟着官网的教程一步一步做下来,在自定义按钮这,始终报如下异常

[ios.RatingControl ratingButtonTapped:]: unrecognized selector sent to instance 0x7fb6d260def0
2016-10-04 14:55:45.377 ios[82888:4851192] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ios.RatingControl ratingButtonTapped:]: unrecognized selector sent to instance 0x7fb6d260def0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e1b534b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010b51621e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e224f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010e13ac15 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010e13a798 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010b9e7b88 -[UIApplication sendAction:to:from:forEvent:] + 83
    6   UIKit                               0x000000010bb6d2b2 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x000000010bb6d5cb -[UIControl _sendActionsForEvents:withEvent:] + 444
    8   UIKit                               0x000000010bb6bf83 -[UIControl touchesBegan:withEvent:] + 414
    9   UIKit                               0x000000010ba54e15 -[UIWindow _sendTouchesForEvent:] + 2043
    10  UIKit                               0x000000010ba567c3 -[UIWindow sendEvent:] + 4011
    11  UIKit                               0x000000010ba03a33 -[UIApplication sendEvent:] + 371
    12  UIKit                               0x000000010c1f5b6d __dispatchPreprocessedEventFromEventQueue + 3248
    13  UIKit                               0x000000010c1ee817 __handleEventQueue + 4879
    14  CoreFoundation                      0x000000010e15a311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x000000010e13f59c __CFRunLoopDoSources0 + 556
    16  CoreFoundation                      0x000000010e13ea86 __CFRunLoopRun + 918
    17  CoreFoundation                      0x000000010e13e494 CFRunLoopRunSpecific + 420
    18  GraphicsServices                    0x000000011005ea6f GSEventRunModal + 161
    19  UIKit                               0x000000010b9e5f34 UIApplicationMain + 159
    20  ios                                 0x000000010af32c0f main + 111
    21  libdyld.dylib                       0x000000010f0df68d start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是自定义的视图类

class RatingControl: UIView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        button.backgroundColor = UIColor.red
        
        // 错误主要是这行,是addTarget这个方法参数错了吗,还是我 ratingButtonTapped 方法写错了,或者是哪里还有什么配置没配好
        button.addTarget(self, action:Selector("ratingButtonTapped:"), for: UIControlEvents.touchDown)
        
        addSubview(button)
    }
    
    // MARK: Button Action
    func ratingButtonTapped(button: UIButton) {
        print("Button pressed ?")
    }

}

我也看不懂这异常信息如何跟踪,不能直接点的吗 T T

解决方案

button.addTarget(self, action:#selector(ratingButtonTapped), for: UIControlEvents.touchDown)

把那句话换成这个试一下。应该是xcode8里面关于selector构造器语法的问题

这篇关于swift - ios自定义按钮报 NSInvalidArgumentException 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆