【问题标题】:Why am I getting unrecognized selector sent to class?为什么我会收到无法识别的选择器发送到课堂?
【发布时间】:2016-07-04 11:33:55
【问题描述】:

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

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:]: 无法识别的选择器发送到类 0x10b564530' *** 首先抛出调用栈:

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

还有:

Selector("hideSelf")

将方法声明为hideSelf()

Selector("hideSelf:")

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

如果我没记错的话,还检查了类继承自 NSObject,这是通过继承 UIView 来完成的。

我正在使用XCode 6.4swift 1.2

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

提前感谢您的帮助。

【问题讨论】:

  • 你用的是什么版本的swift?
  • 非常感谢。

标签: ios swift unrecognized-selector


【解决方案1】:

由于showInParentstatic 函数,您在NSTimer.scheduledTimerWithTimeInterval 中使用的self 指的是类而不是类的实例。如果没有找到正确的目标,iOS 将不会找到实例方法 hideSelf

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

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

【讨论】:

  • 没想到,我将通知的 addObserver 滑入了一个类方法,并没有考虑它。然后在其他地方对 addObserver 和 postNotification 进行更多添加和编辑后,发现我的异常处理被触发了,而 NSNotification 观察者没有。被这种思维方法名称上下文冲突和一堆调试所愚弄……然后发现了这一点,并用已故伟大的詹姆斯布朗的话“天哪!” :-) 我也谢谢你!
【解决方案2】:

试试这个代码。

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

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

【讨论】:

    【解决方案3】:

    改变这一行:

    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)
    

    这应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 2019-08-19
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多