I have set a password field and under that a UIButton
to toggle secureTextEntry
to YES
/NO
.
The following is the code that I have used.
- (void)viewDidLoad
{
self.navigationController.navigationBarHidden=YES;
[_ShowPasswordButtonOutlet setImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];
_password.secureTextEntry=YES;
[super viewDidLoad];
}
- (IBAction)ShowPassword:(id)sender
{
if ([_ShowPasswordButtonOutlet.imageView.image isEqual:[UIImage imageNamed:@"box.png"]])
{
[_ShowPasswordButtonOutlet setImage:[UIImage imageNamed:@"box_tick.png"] forState:UIControlStateNormal];
_password.secureTextEntry=NO;
} else if ([_ShowPasswordButtonOutlet.imageView.image isEqual:[UIImage imageNamed:@"box_tick.png"]])
{
[_ShowPasswordButtonOutlet setImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];
_password.secureTextEntry=YES;
}
}
The Above code seems to be working in iOS7 and iOS7.1.
But in iOS6.1, The button image seems to be changing when clicked but the secureTextEntry
works only once from YES
to NO
. Later if clicked only the UIButton
, the image changes and secureTextEntry
is not working.
It is not showing any warning or errors!
You should disable UITextField
before set secureTextEntry
value to YES
in iOS6.
- (IBAction)ShowPassword:(id)sender
{
if (_password.secureTextEntry)
{
[_ShowPasswordButtonOutlet setImage:[UIImage imageNamed:@"box_tick.png"] forState:UIControlStateNormal];
_password.secureTextEntry = NO;
}
else
{
[_ShowPasswordButtonOutlet setImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];
_password.enabled = NO;
_password.secureTextEntry = YES;
_password.enabled = YES;
}
}