![](/img/trans.png)
[英]Why am I getting an “unrecognized selector sent to class” error for CIFilter?
[英]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.4
和swift 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.