完成button只能用于数字键盘
我正在开发iPhone应用程序。 在一个视图中我有四个文本框。 在这四个文本框中,我有一个文本框,其中将使用数字键盘。 但是在数字键盘上没有返回键。 所以我通过使用下面的代码以编程方式添加了“完成”button。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; } else { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)addButtonToKeyboard { // create custom button UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; doneButton.frame = CGRectMake(0, 163, 106, 53); doneButton.adjustsImageWhenHighlighted = NO; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) { [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal]; [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted]; } else { [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal]; [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted]; } [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; // locate keyboard view UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard found, add the button if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) [keyboard addSubview:doneButton]; } else { if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) [keyboard addSubview:doneButton]; } } } - (void)keyboardWillShow:(NSNotification *)note { // if clause is just an additional precaution, you could also dismiss it if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) { [self addButtonToKeyboard]; } } - (void)keyboardDidShow:(NSNotification *)note { // if clause is just an additional precaution, you could also dismiss it if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { [self addButtonToKeyboard]; } } - (void)doneButton:(id)sender { NSLog(@"doneButton"); NSLog(@"Input: %@", contactno.text); [contactno resignFirstResponder]; }
对于这个特定的文本域来说很好。 但是这个button也是为其他文本框添加的。 如何取消隐藏其他文本框的button。
非常感谢。
请请请请请请请请请请请请请请请不要这样做。 这是非常脆弱的。 考虑一下:
- 如果数字键盘的布局改变怎么办?
- 如果按键的颜色改变了怎么办?
- 如果键盘的执行改变,使其不再是索引1处的窗口?
- 如果键盘和外围设备主机的实现发生改变,那么内省的描述就会中断?
- 如果你在iPad上运行这个键盘有完全不同的布局呢?
这些只是将您的用户界面整合到私有视图层次结构中的各种问题。
不要这样做。
这里有两个替代scheme:
-
在每个
UIResponder
(以及每个UIView
)上使用-inputAccessoryView
属性来定义一个带有“完成”button的UIToolbar
。 当键盘input和输出时,inputAccessoryView
将位于键盘上方。 -
在您的整个用户界面中添加一个透明的
UIView
,捕捉轻按事件并使键盘消失
我想你可以保留一个全局variablesUITextField * selectedTextField
作为指针(弱引用)到用户当前select的textField。 IBOutlets
存储到视图上的所有UITextField
对象(强引用)。 然后将整个addButtonToKeyboard
主体包装在一个大的if子句中:
if (self.selectedTextField == self.contactNumberTextField){ // ... add button }
您将需要设置UITextField
委托方法
- (void)textFieldDidBeginEditing:(UITextField *)textField;
知道哪个文本字段是当前select的字段。
事实上,我认为你甚至可以使用selectedTextField.keyboardType
属性来完成没有sockets的任务,但是,你可能已经有了sockets,因为你需要它们来读取用户的input。
希望这可以帮助!
把这段代码放到你的项目中,只需要设置iboutlet的文本字段。它也有一些未使用的方法,但是你可以使用它来select完成button。然后你将删除未使用的方法。这只是数字键盘上的Get Donebutton
.H文件
@interface aaaViewController : UIViewController { IBOutlet UITextField *txthello; UIImage *numberPadDoneImageNormal; UIImage *numberPadDoneImageHighlighted; UIButton *numberPadDoneButton; } @property (nonatomic, retain) UIImage *numberPadDoneImageNormal; @property (nonatomic, retain) UIImage *numberPadDoneImageHighlighted; @property (nonatomic, retain) UIButton *numberPadDoneButton;
.M文件
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Add listener for keyboard display events if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; } else { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } // Add listener for all text fields starting to be edited [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; } else { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; } [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:nil]; [super viewWillDisappear:animated]; } - (UIView *)findFirstResponderUnder:(UIView *)root { if (root.isFirstResponder) return root; for (UIView *subView in root.subviews) { UIView *firstResponder = [self findFirstResponderUnder:subView]; if (firstResponder != nil) return firstResponder; } return nil; } - (UITextField *)findFirstResponderTextField { UIResponder *firstResponder = [self findFirstResponderUnder:[self.view window]]; if (![firstResponder isKindOfClass:[UITextField class]]) return nil; return (UITextField *)firstResponder; } - (void)updateKeyboardButtonFor:(UITextField *)textField { // Remove any previous button [self.numberPadDoneButton removeFromSuperview]; self.numberPadDoneButton = nil; // Does the text field use a number pad? if (textField.keyboardType != UIKeyboardTypeNumberPad) return; // If there's no keyboard yet, don't do anything if ([[[UIApplication sharedApplication] windows] count] < 2) return; UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; // Create new custom button self.numberPadDoneButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.numberPadDoneButton.frame = CGRectMake(0, 163, 106, 53); self.numberPadDoneButton.adjustsImageWhenHighlighted = FALSE; [self.numberPadDoneButton setTitle:@"Return" forState:UIControlStateNormal]; // [self.numberPadDoneButton setFont:[UIFont boldSystemFontOfSize:18]]; [self.numberPadDoneButton setTitleColor:[UIColor colorWithRed:77.0f/255.0f green:84.0f/255.0f blue:98.0f/255.0f alpha:1.0] forState:UIControlStateNormal]; [self.numberPadDoneButton setImage:self.numberPadDoneImageNormal forState:UIControlStateNormal]; [self.numberPadDoneButton setImage:self.numberPadDoneImageHighlighted forState:UIControlStateHighlighted]; [self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside]; // Locate keyboard view and add button NSString *keyboardPrefix = [[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 ? @"<UIPeripheralHost" : @"<UIKeyboard"; for (UIView *subView in keyboardWindow.subviews) { if ([[subView description] hasPrefix:keyboardPrefix]) { [subView addSubview:self.numberPadDoneButton]; [self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside]; break; } } } - (void)textFieldDidBeginEditing:(NSNotification *)note { [self updateKeyboardButtonFor:[note object]]; } - (void)keyboardWillShow:(NSNotification *)note { [self updateKeyboardButtonFor:[self findFirstResponderTextField]]; } - (void)keyboardDidShow:(NSNotification *)note { [self updateKeyboardButtonFor:[self findFirstResponderTextField]]; } - (IBAction)numberPadDoneButton:(id)sender { UITextField *textField = [self findFirstResponderTextField]; [textField resignFirstResponder]; } - (void)dealloc { [numberPadDoneImageNormal release]; [numberPadDoneImageHighlighted release]; [numberPadDoneButton release]; [super dealloc]; }