我正在爲運行在iOS 8(不適用於應用商店)的iPad創建內部應用程序,其中一項要求是複製iPad解鎖/ PIN碼條目數字鍵盤的定製鍵盤。爲iPad應用程序定製的數字鍵盤
我根據我的自定義數字鍵盤在這個項目上:https://github.com/lnafziger/Numberpad
在項目中,我添加了一個新的UIView到的UIViewController,這樣我可以複製出現在一個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個UITextFields之間自動移動(一個UITextField只能接受1個字符,然後移動到下一個UITextField)。 numberpadNumberPressed方法似乎根本不會觸發更新文本字段。
[END編輯]
我該怎麼做?
爲什麼不直接使用一個'UITextField'設置爲'YES'了'secureTextEntry'場? – quark 2014-10-20 21:02:53
@夸克你能否擴展你的意思,請不要理解?問題是我的水龍頭沒有將數字放入字段中(並且在刪除時也需要刪除)。 – motionpotion 2014-10-20 21:06:03
你可以顯示你的代碼,當你點擊一個數字時會發生什麼?那將是一個開始的好地方。 – 2014-10-20 21:33:06