简体   繁体   English

为什么我会收到发送到 class 的无法识别的选择器?

[英]Why am I getting unrecognized selector sent to class?

I have been going through all stack overflow trying to solve this but none of the solutions work.我一直在尝试解决所有堆栈溢出问题,但没有一个解决方案有效。

class ToastView: UIView {

    static func showInParent(parentView: UIView!, withText text: String, forDuration duration: double_t) {

        //Count toast views are already showing on parent. Made to show several toasts one above another
        var toastsAlreadyInParent = 0;

        for view in parentView.subviews {
            if (view.isKindOfClass(ToastView)) {
                toastsAlreadyInParent++
            }
        }

        var parentFrame = parentView.frame;

        var yOrigin = parentFrame.size.height - getDouble(toastsAlreadyInParent)

        var selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, toastHeight);
        var toast = ToastView(frame: selfFrame)

        toast.textLabel.backgroundColor = UIColor.clearColor()
        toast.textLabel.textAlignment = NSTextAlignment.Center
        toast.textLabel.textColor = UIColor.whiteColor()
        toast.textLabel.numberOfLines = 2
        toast.textLabel.font = UIFont.systemFontOfSize(13.0)
        toast.addSubview(toast.textLabel)

        toast.backgroundColor = UIColor.darkGrayColor()
        toast.alpha = 0.0;
        toast.layer.cornerRadius = 4.0;
        toast.textLabel.text = text;

        parentView.addSubview(toast)
        UIView.animateWithDuration(0.4, animations: {
            toast.alpha = 0.9
            toast.textLabel.alpha = 0.9
        })

        var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self , selector: "hideSelf:", userInfo: nil,     repeats: false)
        //toast.performSelector(Selector("hideSelf"), withObject: nil, afterDelay: duration)

    }

    static private func getDouble(toastsAlreadyInParent : Int) -> CGFloat {
        return (70.0 + toastHeight * CGFloat(toastsAlreadyInParent) + toastGap * CGFloat(toastsAlreadyInParent));
    }

    func hideSelf( timer: NSTimer) {
        UIView.animateWithDuration(0.4, animations: {
            self.alpha = 0.0
            self.textLabel.alpha = 0.0
            }, completion: { t in self.removeFromSuperview() })
    }

} 

This is the error I get:这是我得到的错误:

NSInvalidArgumentException', reason: '+[HonorsApp.ToastView hideSelf:]: unrecognized selector sent to class 0x10b564530' *** First throw call stack: NSInvalidArgumentException',原因:'+[HonorsApp.ToastView hideSelf:]:无法识别的选择器发送到 class 0x10b564530' *** 首先抛出调用堆栈:

I have tried adding @objc to the method called from selector and to the class but it has been useless我尝试将 @objc 添加到从选择器调用的方法和 class 中,但它没有用

Also:还:

Selector("hideSelf")

declaring the method as hideSelf()将方法声明为hideSelf()

Selector("hideSelf:")

declaring the method as hideSelf( timer: NSTimer)将方法声明为hideSelf( timer: NSTimer)

have also checked that the class inherits from NSObject which is accomplished by inheriting from UIView, if I am not wrong.还检查了 class 是否继承自 NSObject,如果我没记错的话,它是通过继承自 UIView 来实现的。

I am using XCode 6.4 and swift 1.2我正在使用XCode 6.4swift 1.2

I am new at programming in swift and needed something like the Toast function in android and I found this code but as soon as i hit run the error came up.我是 swift 的编程新手,需要 android 中的 Toast function 之类的东西,我找到了这段代码,但是一旦我点击运行,错误就出现了。

Thanks for the help in advance.我在这里先向您的帮助表示感谢。

Since showInParent is a static function, the self you are using in NSTimer.scheduledTimerWithTimeInterval refers to the class and not an instance of the class. 由于showInParent是一个static功能, self您使用的是NSTimer.scheduledTimerWithTimeInterval指的是类,而不是类的实例。 iOS isn't going to find the instance method hideSelf if it isn't looking in the right target. 如果没有查找正确的目标,iOS将不会找到实例方法hideSelf

Try passing the instance of ToastView that you create to the timer: 尝试将您创建的ToastView实例传递给计时器:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: toast, selector: "hideSelf:", userInfo: nil, repeats: false)

try this code. 试试这段代码。

toast.performSelector(Selector("hideSelf:"), withObject: nil) or toast.performSelector(Selector("hideSelf:"), withObject: nil)

toast.performSelector(#selector(ClassName.hideSelf(_:)), withObject: nil) s

Change this line: 改变这一行:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self , selector: "hideSelf:", userInfo: nil, repeats: false)

into: 成:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: toast , selector: "hideSelf:", userInfo: nil, repeats: false)

This should help. 这应该有所帮助。

I got this error earlier today.我今天早些时候收到这个错误。 Xcode suggested putting this: Xcode 建议把这个:

self' refers to the method 'LoginController.self', which may be unexpected Use 'LoginController.self' to silence this warning self' 引用 'LoginController.self' 方法,这可能是意外使用 'LoginController.self' 来消除此警告

But turns out the LoginController.self does not work.但事实证明LoginController.self不起作用。 This is a bug in Xcode这是 Xcode 中的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到CIFilter的“无法识别的选择器发送给类”错误? - Why am I getting an “unrecognized selector sent to class” error for CIFilter? 为什么我得到一个“无法识别的选择器发送到实例”错误? - Why am I getting a `unrecognized selector sent to instance` error? 为什么我得到这个:_cfurl:无法识别的选择器 - Why am I getting this: _cfurl: unrecognized selector 为什么我会收到“ [__NSArrayI allKeys]:无法识别的选择器发送到实例” /为什么要进行NSDictionary转换? - Why am I getting “[__NSArrayI allKeys]: unrecognized selector sent to instance” / why is NSDictionary transforming? 为什么我的MapViewController收到无法识别的选择器发送到实例异常? (iOS) - Why am I getting a unrecognized selector sent to instance exception with my MapViewController? (iOS) 选择器语法:为什么我得到无法识别的选择器错误? - selector syntax: why I am getting unrecognized selector error? 无法识别的选择器已发送给班级 - Unrecognized selector sent to class Swift 无法识别的选择器发送到实例 - 我错过了什么? - Swift unrecognized selector sent to instance - What am I missing? NSData 无法识别的选择器发送到类 - NSData Unrecognized selector sent to class 无法识别的选择器已发送给班级(单人) - Unrecognized selector sent to class (singleton)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM