Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

I have tried adding @objc to the method called from selector and to the class but it has been useless

Also:

Selector("hideSelf")

declaring the method as hideSelf()

Selector("hideSelf:")

declaring the method as hideSelf( timer: NSTimer)

have also checked that the class inherits from NSObject which is accomplished by inheriting from UIView, if I am not wrong.

I am using XCode 6.4 and swift 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.

Thanks for the help in advance.

like image 952
luis enrique mesias flores Avatar asked Jul 04 '16 11:07

luis enrique mesias flores


1 Answers

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. iOS isn't going to find the instance method hideSelf if it isn't looking in the right target.

Try passing the instance of ToastView that you create to the timer:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: toast, selector: "hideSelf:", userInfo: nil, repeats: false)
like image 177
vacawama Avatar answered Oct 27 '22 01:10

vacawama