Do not show keyboard while listening to text field

I have UITextField and UIDatePicker in my XIB file, now I have assigned the date selected in UIDatePicker to the value of the text in the text box. I did it well, my only problem is that I want the keyboard not to appear when I click on the text box. I am sure that someone knows the answer, I will be glad if you share it :) Thank you!

by the way. I tried this, but it did not work.

 - (void)textFieldDidBeginEditing:(UITextField *)textField { [textField resignFirstResponder]; } 
+4
source share
5 answers

Just set UIDatePicker as inputView of your UITextField .

This will display the keyboard as an input view when the user clicks on a UITextField using a UIDatePicker .

 self.dateTextField.inputView = self.datePicker; 
+12
source

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

+4
source

Try the following:

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

Remember to make yourViewController compatible with <UITextFieldDelegate> , and set yourTextField.delegate = self;

+3
source

Since I did not see the complete answer, I also need it, and I will come to a solution with some answers, here I will lay out my code.

Just put some β€œempty” UIView as input view.

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

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

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

and i still used

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

Thank you for your help! Hope this thread helps others.

-2
source

Source: https://habr.com/ru/post/1489431/


All Articles