title: Custom Number Pad For iPad App
body: <p>I'm creating an in-house app for iPad running on iOS 8 (not for app store) and one of the requirements is a custom keyboard that replicates the iPad unlock/PIN code entry number pad.</p>
  
  <p>I based my custom number pad on this project: <a href="https://github.com/lnafziger/Numberpad" rel="nofollow noreferrer">https://github.com/lnafziger/Numberpad</a></p>
  
  <p>In my project I have added a new UIView to the UIViewController so that I can replicate the dots that appear when a 4-digit PIN code is typed as below:</p>
  
  <p><img src="https://i.stack.imgur.com/dIxH1.png" alt="Custom Number Pad with PIN text boxes."></p>
  
  <p>My problem is that I cannot get the targeting of the numbers correct when I tap them.  My goal is that when I tap the numbers 0 to 9 that the first tap will put that number into the first text box and the second into the second etc.  But I cannot get the taps on the number pad to register in any of the 4 available UITextFields.</p>
  
  <p>[EDIT]</p>
  
  <p>Here is that part of the code so far:</p>
  
  <pre><code>#pragma mark - KEYPAD IBACTIONS
  
  // A number (0-9) was just pressed on the number pad
  // Note that this would work just as well with letters or any other character and is not limited to numbers.
  - (IBAction)numberpadNumberPressed:(UIButton *)sender {
      if (self.targetTextInput) {
          NSString *numberPressed  = sender.titleLabel.text;
          if ([numberPressed length] &gt; 0) {
              UITextRange *selectedTextRange = self.targetTextInput.selectedTextRange;
              if (selectedTextRange) {
                  [self textInput:self.targetTextInput replaceTextAtTextRange:selectedTextRange withString:numberPressed];
              }
          }
      }
  }
  
  // The delete button was just pressed on the number pad
  - (IBAction)numberpadDeletePressed:(UIButton *)sender {
      if (self.targetTextInput) {
          UITextRange *selectedTextRange = self.targetTextInput.selectedTextRange;
          if (selectedTextRange) {
              // Calculate the selected text to delete
              UITextPosition  *startPosition  = [self.targetTextInput positionFromPosition:selectedTextRange.start offset:-1];
              if (!startPosition) {
                  return;
              }
              UITextPosition  *endPosition    = selectedTextRange.end;
              if (!endPosition) {
                  return;
              }
              UITextRange     *rangeToDelete  = [self.targetTextInput textRangeFromPosition:startPosition
                                                                                 toPosition:endPosition];
  
              [self textInput:self.targetTextInput replaceTextAtTextRange:rangeToDelete withString:@""];
          }
      }
  }
  
  // The clear button was just pressed on the number pad
  - (IBAction)numberpadClearPressed:(UIButton *)sender {
      if (self.targetTextInput) {
          UITextRange *allTextRange = [self.targetTextInput textRangeFromPosition:self.targetTextInput.beginningOfDocument
                                                                       toPosition:self.targetTextInput.endOfDocument];
  
          [self textInput:self.targetTextInput replaceTextAtTextRange:allTextRange withString:@""];
      }
  }
  
  // The done button was just pressed on the number pad
  - (IBAction)numberpadDonePressed:(UIButton *)sender {
      if (self.targetTextInput) {
          [self.targetTextInput resignFirstResponder];
      }
  }
  
  - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  {
      return YES;
  }
  
  - (BOOL)textViewShouldEndEditing:(UITextView *)textView
  {
      return YES;
  }
  
  // Replace the text of the textInput in textRange with string if the delegate approves
  - (void)textInput:(id &lt;UITextInput&gt;)textInput replaceTextAtTextRange:(UITextRange *)textRange withString:(NSString *)string {
      if (textInput) {
          if (textRange) {
              // Calculate the NSRange for the textInput text in the UITextRange textRange:
              int startPos                    = [textInput offsetFromPosition:textInput.beginningOfDocument
                                                                   toPosition:textRange.start];
              int length                      = [textInput offsetFromPosition:textRange.start
                                                                   toPosition:textRange.end];
              NSRange selectedRange           = NSMakeRange(startPos, length);
  
              if ([self textInput:textInput shouldChangeCharactersInRange:selectedRange withString:string]) {
                  // Make the replacement:
                  [textInput replaceRange:textRange withText:string];
              }
          }
      }
  }
  
  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
      NSString *passcode = [textField text];
      passcode = [passcode stringByReplacingCharactersInRange:range withString:string];
  
      switch ([passcode length]) {
          case 0:
              self.txtBulletField0.text = nil;
              self.txtBulletField1.text = nil;
              self.txtBulletField2.text = nil;
              self.txtBulletField3.text = nil;
              break;
          case 1:
              self.txtBulletField0.text = @"*";
              self.txtBulletField1.text = nil;
              self.txtBulletField2.text = nil;
              self.txtBulletField3.text = nil;
              break;
          case 2:
              self.txtBulletField0.text = @"*";
              self.txtBulletField1.text = @"*";
              self.txtBulletField2.text = nil;
              self.txtBulletField3.text = nil;
              break;
          case 3:
              self.txtBulletField0.text = @"*";
              self.txtBulletField1.text = @"*";
              self.txtBulletField2.text = @"*";
              self.txtBulletField3.text = nil;
              break;
          case 4:
              self.txtBulletField0.text = @"*";
              self.txtBulletField1.text = @"*";
              self.txtBulletField2.text = @"*";
              self.txtBulletField3.text = @"*";
  
              // Notify delegate a little later so we have a chance to show the 4th bullet
              [self performSelector:@selector(notifyDelegate:) withObject:passcode afterDelay:0];
  
              return NO;
  
              break;
          default:
              break;
      }
  </code></pre>
  
  <p>In the original project the number pad targeted one UITextField and put all the number taps into there.  Now I want to have it automatically move between my 4 UITextFields (one UITextField should only accept 1 character and then move to the next UITextField).  The numberpadNumberPressed method does not seem to trigger updating the text fields at all. </p>
  
  <p>[END EDIT]</p>
  
  <p>How would I do this?</p>