我一直在查看所有的Stack Overflow,试图解决这个问题,但是没有任何解决方案有效。
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函数一样的东西,我找到了这段代码,但是当我点击运行时出现了错误。
提前感谢您的帮助。