【发布时间】: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.4 和swift 1.2
我是 swift 编程的新手,需要类似 android 中的 Toast 函数,我找到了这段代码,但是当我点击运行时,错误就出现了。
提前感谢您的帮助。
【问题讨论】:
-
你用的是什么版本的swift?
-
非常感谢。
标签: ios swift unrecognized-selector