在iPhone应用程序中,我有UITextField
需要输入作为数字,所以我将键盘类型传递为UIKeyboardTypeNumberPad
。它有效,但没有返回或完成按钮,所以我怎么能隐藏键盘。
参考代码是:
textDriverAge.delegate=self;
textDriverAge.keyboardType=UIKeyboardTypeNumberPad;
textDriverAge.clearButtonMode = UITextFieldViewModeWhileEditing;
[textDriverAge setReturnKeyType:UIReturnKeyDone];// UIReturnKeyNext];
textDriverAge.enablesReturnKeyAutomatically=TRUE;
我处理了UITextfield
事件,例如:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textDriverAge == textField)
{
[textDriverAge resignFirstResponder];
//[tglDropInDropOff becomeFirstResponder];
}
}
指导我如何隐藏键盘。
答案 0 :(得分:2)
你可以使用两种类型
Touch Method
。
在另一个选项中,在视图控制器中添加UIToolbar
。
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
nil];
yourtextField.inputAccessoryView = numberToolbar;
}
- (IBAction)cancelNumberPad:(UITextField*)textField
{
[yourtextField resignFirstResponder];
yourtextField.text=@"";
}
- (IBAction)doneWithNumberPad:(UITextField*)textField
{
[yourtextField resignFirstResponder];
}
答案 1 :(得分:1)
试试这个..: - )
- (void)viewDidLoad
{
[self addTapGesture];
}
#pragma mark
#pragma mark Gesture methods
- (void) addTapGesture {
UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapper.cancelsTouchesInView = FALSE;
[self.view addGestureRecognizer:tapper];
}
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
答案 2 :(得分:0)
您需要将inputAccesoryView放入文本字段并向其添加完成按钮。在这里,我创建了一个xib名称'InputAccessoryView',并根据我的需要进行设计。你也可以这样做......
InputAccessoryView *inputAccessoryView=(InputAccessoryView *)[[[NSBundle mainBundle] loadNibNamed:@"InputAccessoryView" owner:self options:nil] objectAtIndex:0];
[inputAccessoryView.doneButton setTarget:self];
[inputAccessoryView.doneButton setAction:@selector(accessoryDoneAction:)];
[inputAccessoryView.cancelButton setTarget:self];
[inputAccessoryView.cancelButton setAction:@selector(acessoryCancelButtonAction:)];
[self.textField setInputAccessoryView:inputAccessoryView];
答案 3 :(得分:0)
添加以下代码以在numberpad上添加返回按钮
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Return" style:UIBarButtonItemStyleBordered target:self action:@selector(returnNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)returnNumberPad
{
[numberTextField resignFirstResponder];
}