我正在为iOS 8上运行的iPad创建一个内部应用程序(不适用于应用程序商店),其中一个要求是复制iPad解锁/ PIN码输入数字键盘的自定义键盘。
我将自定义数字键盘基于此项目:https://github.com/lnafziger/Numberpad
在我的项目中,我向UIViewController添加了一个新的UIView,以便我可以复制下面输入4位数PIN码时出现的点:
我的问题是,当点按它们时,我无法正确定位数字。我的目标是,当我点击数字0到9时,第一个点击将该数字放入第一个文本框,第二个点击进入第二个等等。但我无法在数字键盘上点击以在4中的任何一个中注册可用的UITextFields。
[编辑]
到目前为止,这是代码的一部分:
#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;
}
在原始项目中,数字键盘指向一个UITextField并将所有数字抽头放入其中。现在我想让它自动在我的4个UITextField之间移动(一个UITextField应该只接受1个字符,然后移动到下一个UITextField)。 numberpadNumberPressed方法似乎根本不会触发更新文本字段。
[结束编辑]
我该怎么做?
答案 0 :(得分:2)
这可能很简单。
只需保留已在一个可变字符串变量中点击的数字。按下某个键后,添加下一个数字。如果按下删除键,则删除最后一个号码(如果有)。这应该很容易。 E.g。
if (pinString.length < 4) {
[pinString appendString:sender.titleLabel.text]
}
和
if (pinString.length) {
[pinString deleteCharactersInRange:NSMakeRange(pinString.length-1,1)];
}
您没有在文本字段中编辑数字,因此您应该使用标签。在下文中,我假设您已为其提供了标记101
,102
,103
和104
。
然后就这样做:
for (UILabel *label in theFourTextLabels) {
label.text = label.tag - 100 > pinString.length ? "" : "*";
}