2016-08-04 1 views
0

Mon application se bloque et jette cette erreursélecteur Unrecognized envoyé à l'instance, la raison: « - [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 

Je suis en train de faire une classe personnalisée pour masquer et afficher le mot de passe afin que je puisse utiliser dans toute UITextField dans 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() 
    } 
} 

Répondre

0

Vous ne pouvez pas utiliser avec de nombreux paramètres de sélection hideShowPasswordTextField(_:hideText:) pour UIButton cible. Vous ne pouvez utiliser sélecteur avec un (ou zéro) paramètre, dans lequel UIButton va se mettre: hideShowPasswordTextField(_:)

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

Pour utiliser hideText dans cette fonction, vous pouvez le déclarer comme propriété de votre classe:

var hideText: UITextField! 

et lui attribuer une valeur dans l'étape d'initialisation de classe

+0

En faisant cela, en cliquant sur le bouton masquer et afficher l'image, je suis en mesure de voir le texte en appuyant longuement sur le bouton pour masquer et afficher. En cliquant une fois et en quittant, le texte revient à l'état caché. –

+1

Merci. Ça marche. J'ai dû changer UIControlEvents.AllTouchEvents à touchUpInside –