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

Why am I getting unrecognized selector sent to class?

我一直在尝试解决所有堆栈溢出问题,但 none 的解决方案有效。

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

} 

这是我得到的错误:

NSInvalidArgumentException', reason: '+[HonorsApp.ToastView hideSelf:]: unrecognized selector sent to class 0x10b564530' *** First throw call stack:

我试过将@objc 添加到从选择器调用的方法和 class 但它没用

另外:

Selector("hideSelf")

将方法声明为 hideSelf()

Selector("hideSelf:")

将方法声明为 hideSelf( timer: NSTimer)

还检查了 class 是否继承自 NSObject,如果我没记错的话,它是通过继承自 UIView 来实现的。

我正在使用 XCode 6.4swift 1.2

我是 swift 的编程新手,需要 android 中的 Toast 函数之类的东西,我找到了这段代码,但是当我点击 运行 时,错误就出现了。

提前感谢您的帮助。

试试这个代码。

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

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

由于 showInParent 是一个 static 函数,您在 NSTimer.scheduledTimerWithTimeInterval 中使用的 self 指的是 class 而不是 class。 iOS 如果没有在正确的目标中查找,则不会找到实例方法 hideSelf

尝试将您创建的 ToastView 的实例传递给计时器:

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

更改此行:

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

进入:

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

这应该有所帮助。