2016-07-04 3 views
3

私はこれを解決しようとしているすべてのスタックオーバーフローを行ってきましたが、ソリューションのどれも動作しません。未確認セレクタがクラスに送信されるのはなぜですか?

NSInvalidArgumentException」、理由: '+ [HonorsApp.ToastView hideSelf:]:認識されていないセレクタは、クラス0x10b564530' ***まずスローコールに送ら

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() }) 
    } 

} 

この

は私が取得エラーですスタック:

私は、セレクタからクラスに呼び出されるメソッドに@objcを追加しようとしたが、それは無駄になってい

また:hideSelf()hideSelf(timer: NSTimer)

があれば、クラスはUIViewの継承することによって達成されるNSObjectのから継承することも確認されたような方法を宣言

Selector("hideSelf:") 

の方法を宣言

Selector("hideSelf") 

私は間違っていません。

私はアンドロイドでトースト機能などの迅速かつ必要なもので、プログラミングに新しいですし、私はこのコードを見つけたが、すぐに私はエラーを実行してヒットとしてやってきXCode 6.4swift 1.2

を使用しています。

ご協力ありがとうございます。

+0

あなたはどのバージョンのスイフトを使用していますか? –

+0

本当にありがとうございました。 –

答えて

7

static関数である、NSTimer.scheduledTimerWithTimeIntervalで使用しているselfクラスはなく、クラスのインスタンスを指します。 iOSは、適切なターゲットで探していない場合、インスタンスメソッドhideSelfを見つけられません。

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

ありがとうございました –

+0

あなたは大歓迎です! – vacawama

+0

私はaddObserverにクラスメソッドへの通知を送りましたが、それについては考えませんでした。その後、addObserverとpostNotificationの追加や編集が、他の場所で、例外処理が開始され、NSNotificationのオブザーバが実行されていないことが判明しました。この思考方法の名前のコンテクストの衝突やデバッグの束に惑わされた...そして、これを発見し、後期の偉大なジェームズ・ブラウンの言葉で「良い神! :-)私もありがとう! –

0

このコードを試してください。 showInParentので

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

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

変更するには、この行を::

あなたはタイマーを作成ToastViewのインスタンスを渡してみ

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) 

関連する問題