繁体   English   中英

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

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

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

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',原因:'+[HonorsApp.ToastView hideSelf:]:无法识别的选择器发送到 class 0x10b564530' *** 首先抛出调用堆栈:

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

还:

Selector("hideSelf")

将方法声明为hideSelf()

Selector("hideSelf:")

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

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

我正在使用XCode 6.4swift 1.2

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

我在这里先向您的帮助表示感谢。

由于showInParent是一个static功能, self您使用的是NSTimer.scheduledTimerWithTimeInterval指的是类,而不是类的实例。 如果没有查找正确的目标,iOS将不会找到实例方法hideSelf

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

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

试试这段代码。

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

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

改变这一行:

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)

这应该有所帮助。

我今天早些时候收到这个错误。 Xcode 建议把这个:

self' 引用 'LoginController.self' 方法,这可能是意外使用 'LoginController.self' 来消除此警告

但事实证明LoginController.self不起作用。 这是 Xcode 中的错误

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM