gpt4 book ai didi

ios - iPad 应用程序的自定义数字键盘

转载 作者:行者123 更新时间:2023-11-28 21:56:09 26 4
gpt4 key购买 nike

我正在为在 iOS 8 上运行的 iPad 创建一个内部应用程序(不适用于应用程序商店),其中一个要求是复制 iPad 解锁/PIN 码输入数字键盘的自定义键盘。

我的自定义数字键盘基于这个项目:https://github.com/lnafziger/Numberpad

在我的项目中,我向 UIViewController 添加了一个新的 UIView,这样我就可以复制在键入 4 位 PIN 码时出现的点,如下所示:

Custom Number Pad with PIN text boxes.

我的问题是,当我点击数字时,我无法正确定位数字。我的目标是,当我点击数字 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 方法似乎根本不会触发更新文本字段。

[结束编辑]

我该怎么做?

最佳答案

这可以很简单。

只需将已点击的数字保存在一个 可变字符串变量中。当按下一个键时,添加下一个数字。如果按下删除键,则删除最后一个数字(如果有)。那应该很容易。例如

if (pinString.length < 4) {
[pinString appendString:sender.titleLabel.text]
}

if (pinString.length) {
[pinString deleteCharactersInRange:NSMakeRange(pinString.length-1,1)];
}

您没有编辑文本字段中的数字,因此您应该改用标签。在下文中,我假设您已经为他们提供了标签 101 , 102 , 103104 .

然后就做这样的事情:

for (UILabel *label in theFourTextLabels) {
label.text = label.tag - 100 > pinString.length ? "" : "*";
}

关于ios - iPad 应用程序的自定义数字键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com