0

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!

Spidy
  • 1,137
  • 3
  • 28
  • 48
  • 2
    You are comparing `IUIImages`? Don't do that, use a boolean flag or just check `_password.secureTextEntry`. Also consider using a UISwitch` instead of a `UIButton`, it has a state. Also by convention method and variable names begin with a lowercase letter. – zaph May 21 '14 at 12:18
  • Ok I use Boolean flags. Thank you for the Info. – Spidy May 21 '14 at 12:24

1 Answers1

1

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;
    }
}
ujell
  • 2,792
  • 3
  • 17
  • 23
  • Don't compare images, in place of a boolean. – zaph May 21 '14 at 12:24
  • Ok. Disabling the `UITextField` worked. I was just wondering why this is an issue only in iOS6.1 and less as it was working fine in iOS7.0 and above. – Spidy May 21 '14 at 12:26
  • @Zaph you are right, since the answer was not directly about that part, i didn't pay attention to it. Edited it now, thanks. – ujell May 21 '14 at 12:35