Creating a dialogue with a pin code

I would like to create a dialogue with a pin code, for example one that you can enable on iPhone.

For those who have not seen this, it consists of four drawers and a numeric keypad. When you enter a number, a dot appears in the first field. Etc. When you click the Delete button, the last point will be deleted.

I have this set as four UITextFields, and in my delegate I listen:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  [self performSelector:@selector(pickNext:) withObject:textField afterDelay:0.0];
  return YES;
}

PickNext: method will switch to the next UITextField, for example:

- (void)pickNext:(UITextField*)textField
{
  switch ([textField tag]) {
    case 1:
      [pin2 becomeFirstResponder];
      break;
    case 2:
      [pin3 becomeFirstResponder];
      break;
    case 3:
      [pin4 becomeFirstResponder];
      break;
    case 4:
      [textField resignFirstResponder];
      break;
    default:
      break;
  }
}

It really works, but the problem for me is that the delete key does not cause any notifications when the UITextField is already empty. Therefore, I can not go to the previous UITextField.

And someone has a better solution how to solve this problem. I think a hidden text box ... ??

+5
2

, . . , , , .

, :

viewDidLoad:

[hidden becomeFirstResponder];

" " UITextField . :

- (IBAction)textChanged:(UITextField*)hiddenField
{
  NSString *hiddenText = hiddenField.text;

  [self setOneTextField:pin1 toString:hiddenText atIndex:0];
  [self setOneTextField:pin2 toString:hiddenText atIndex:1];
  [self setOneTextField:pin3 toString:hiddenText atIndex:2];
  [self setOneTextField:pin4 toString:hiddenText atIndex:3];
}

- (void)setOneTextField:(UITextField*)textField toString:(NSString*)string atIndex:(NSInteger)index
{
  if ([string length] > index)
    textField.text = [string substringWithRange:NSMakeRange(index, 1)];
  else
    textField.text = @""; 
}

UITextField , "shouldChangeCharactersInRange":

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  bool okToEdit = YES;

  if (range.location > 3)
  {
    okToEdit = NO;
  } else if (range.location == 3) {
    [self performSelector:@selector(sendPinCodeNotification) withObject:nil afterDelay:0.0];
  }
  return okToEdit;
}

- (void)sendPinCodeNotification
{
  [[NSNotificationCenter defaultCenter] postNotificationName:PINCODE_NOTIFICATION object:[NSString stringWithFormat:@"%@%@%@%@", pin1.text, pin2.text, pin3.text, pin4.text]];
}

, , .

+7

, UITextField "ValueChanged" , 0.

ValueChanged UIText , . .

-(IBAction) pinChanged: (id)sender {

    UITextField *currentField = (UITextField*) sender;

    // if the field thqt has just been changed is blank
    if ([currentField.text length] == 0) {

        // switch on the fields tag, and go to the previous field
        switch (currentField.tag) {
            case 1:
                // in first field already, stay here!
                break;
            case 2:
                // go back to previous field
                [pin1 becomeFirstResponder];
                break;
            case 3:
                // go back to previous field
                [pin2 becomeFirstResponder];
                break;
            case 4:
                // go back to previous field
                [pin3 becomeFirstResponder];
                break;
            default:
                break;
        }
    }
}
+1

All Articles