2016-08-04 14 views
0

マイアプリのクラッシュを、私は非表示のためのカスタムクラスを作成し、私が使用できるように、パスワードを表示しようとしている認識されないセレクタはインスタンスに、その理由送られた:「 - [UITouchesEvent rightView]

 
[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e0 
2016-08-04 17:07:15.569 [3809:1375151] 

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e0' 

*** First throw call stack: 
(0x180d72db0 0x1803d7f80 0x180d79c4c 0x180d76bec 0x180c74c5c 0x1000e681c 0x1000e6a60 0x185f08be8 0x185f08b64 0x185ef0870 0x185f11360 0x185f07ed8 0x185f00c20 0x185ed104c 0x185ecf628 0x180d2909c 0x180d28b30 0x180d26830 0x180c50c50 0x182538088 0x185f3a088 0x1000f5c88 0x1807ee8b8) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

このエラーがスローされます任意のUITextFieldであれば、UIViewControllerになります。

class HideShowIcon:NSObject { 

    var showPasswordImage = UIImage(named: "ic_show_password") as UIImage? 
    var hidePasswordImage = UIImage(named: "ic_hide_password") as UIImage? 


    func hideShowPasswordButton(hideText:UITextField) { 

     var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)]) 
     var hideShow: UIButton = UIButton(type: UIButtonType.System) 
     hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: hideText.frame.size.height) 
     hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal) 
     hideText.rightView = hideShow 
     hideText.rightViewMode = UITextFieldViewMode.Always 
     hideShow.addTarget(self, action: #selector(HideShowIcon.hideShowPasswordTextField(_:hideText:)), forControlEvents: UIControlEvents.AllTouchEvents) 

    } 
    func hideShowPasswordTextField(sender: AnyObject,hideText:UITextField) { 

     var hideShow: UIButton = (hideText.rightView as? UIButton)! 
     if !hideText.secureTextEntry { 
      hideText.secureTextEntry = true 

      hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal) 
     } else { 
      hideText.secureTextEntry = false 
      hideShow.setImage(showPasswordImage, forState: UIControlState.Normal) 
     } 
     hideText.becomeFirstResponder() 
    } 
} 

答えて

0

あなたは多くのパラメータUIButtonターゲットのhideShowPasswordTextField(_:hideText:)でセレクタを使用することはできません。あなたが唯一れるUIButtonは自分自身を置く、1つ(またはゼロ)のパラメータをセレクタを使用することができます。

var hideText: UITextField! 
hideShowPasswordTextField(_:)

func hideShowPasswordTextField(sender: UIButton) { 
    //... 
} 

をこの機能でhideTextを使用するには、クラスのプロパティとしてそれを宣言することができます

クラスの初期化ステップで値を割り当てます

+0

これを行うには、hide and show imageボタンをクリックしながら、ボタンを押して表示を隠すためにテキストを長時間表示することができます。一度クリックすると、テキストが隠し状態に戻ります。 –

+1

ありがとうございます。できます。 UIControlEvents.AllTouchEventsをtouchUpInsideに変更しなければならなかった –

関連する問題