Reputation: 2736
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.
I based my custom number pad on this project: https://github.com/lnafziger/Numberpad
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:
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.
[EDIT]
Here is that part of the code so far:
#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] > 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 <UITextInput>)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;
}
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.
[END EDIT]
How would I do this?
Upvotes: 0
Views: 1322
Reputation: 80265
This could be so simple.
Just keep the numbers that have been tapped in one mutable string variable. When a key is pressed, add the next number. If the delete key is pressed delete the last number, if any. That should be easy. E.g.
if (pinString.length < 4) {
[pinString appendString:sender.titleLabel.text]
}
and
if (pinString.length) {
[pinString deleteCharactersInRange:NSMakeRange(pinString.length-1,1)];
}
You are not editing the numbers in the text fields, so you should use labels instead. In the following, I assume you have given them the tags 101
, 102
, 103
and 104
.
Then just do something like this:
for (UILabel *label in theFourTextLabels) {
label.text = label.tag - 100 > pinString.length ? "" : "*";
}
Upvotes: 2