I have a UITextField and a UIDatePicker in my XIB file, now I assigned the date that is chosen on the UIDatePicker to be the value of the text in my textfield. I've done this well, my only problem now is that I want the keyboard not to show when I tap on the textfield. I'm sure somebody knows the answer, I will be glad if you will share it :) Thanks!

btw. I tried this, but it didn't work..

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
有帮助吗?

解决方案

Just set the UIDatePicker as the inputView of your UITextField.

This wil repalce the keyboard as the input view when the user clicks on the UItextField with the UIDatePicker.

self.dateTextField.inputView = self.datePicker;

其他提示

You can set delegate in your UITextField and return NO in textFieldShouldBeginEditing.

Try this:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == YOU_TEXT_FIELD_TAG) {
        return NO;
    }
    return YES;
}

Don't forget to make yourViewController conform to <UITextFieldDelegate> and also set yourTextField.delegate = self;

Since I didn't saw full answer, that I also needed, and come to solution with some answers here I'll put my code.

Simply put some "blank" UIView as input view.

Objective C

  UIView *hideKeyboardView = [[UIView alloc]init];
  self.yourTextField.inputView = hideKeyboardView;

Swift

let hideKeyboardView = UIView()
yourTextField.inputView = hideKeyboardView

I already fixed it! I added the following code on the viewDidLoad

    [self.dateField addTarget:self action:@selector(textFieldDidBeginEditing:) 
forControlEvents:UIControlEventEditingDidBegin];

and I still used

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

Thanks for all your help anyway! Hope this thread will help others..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top