Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating pin-code dialogue

I would like to create a pin-code dialogue, like the one you can switch on on the iPhone.

For those who have not seen it, it consists of four boxes and a number keypad. When you enter a digit, a dot appears in the first box. And so forth. When you hit the delete button, the last dot is removed.

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

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

The pickNext: method will switch to the next UITextField, like this:

- (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;
  }
}

This actually works, but the problem for me is that the delete key does not produce any notification when the UITextField is already empty. So I have no way of moving to the previous UITextField.

So does anyone have a better sugestion of how to solve this problem. I'm thinking hidden textfield...??

like image 733
Kobski Avatar asked Aug 23 '09 15:08

Kobski


People also ask

How to create (set) own PIN code?

Create PIN code screen should give ability for user to create (set) own PIN code. For successful creation PIN code user will be needed to enter same PIN code twice and show a message with text “Your PIN code is successfully created”. See screenshots below. Authentication by PIN code screen should give ability to enter user’s PIN code.

How do I open the dialogue window in Windows 10?

The dialogue window is visible on the taskbar and can be selected from there or it can be brought to the front of other open windows with the keyboard combination ALT+TAB. What is my national ID or personal code number?

What to do if you get Windows Hello error during pin creation?

Windows Hello errors during PIN creation. When you set up Windows Hello in Windows 10, you may get an error during the Create a PIN step. This topic lists some of the error codes with recommendations for mitigating the problem. If you get an error code that is not listed here, contact Microsoft Support.

How to create a dialog box or window in HTML?

How to create a dialog box or window in HTML ? In this article, we will create a dialog box or window using the <dialog> tag in the document. This tag is used to create popup dialog and models on a web page. This tag is new in HTML5. Attention reader!


1 Answers

OK, so I solved it myself. The hidden textfield was the way to go. Even though it is hidden, you can still make it the first responder and the keyboard will pop up.

So to summarize:

In viewDidLoad:

[hidden becomeFirstResponder];

And then I listen for the "Editing Changed" event and update the four visible UITextField with one character each. Like this:

- (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 = @""; 
}

To restrict the number of characters in the hidden UITextField to four characters I implement the delegate method "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]];
}

And as you can see I send a notification when the fourth digit has been entered.

like image 185
Kobski Avatar answered Sep 21 '22 19:09

Kobski