How to hide the keyboard in objective-c - cocoa-touch

I have a simple view with a textbox and a UIButton. When I click the UIButton I simply want to hide the keyboard that is currently in the view. Is this a simple delegate I can add to the controller or something more complex?
Of the answers that exist on SO already I haven't found one that has a full solution for this context. Any help would be great!

Try something like:
[TextField resignFirstResponder];
Where TextField is the name of your text field.

This is how you hide the UITextField when you hit the return button:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
return YES;
}
This is how you hide when you hit an UIButton:
- (void)hideTextField:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
}

Related

Programmatic UITextField and programmatically releasing the keyboard

I've looked in many places and it seems everyone uses IB. I like it, but find it more fun writing it all out. This being said, I'm having difficulty dropping my keyboard after editing is done. Here's an example.
-(void)textStuff
{
UITextField *someField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];//not true size
}
Here I've tried the resign firstresponder and even a BOOL to say are you editing, YES, then make that keyboard first responder dangit....oh, you're done editing...good, now resign that first responder.....none has worked as of yet. Any help would be greatly appreciated.
Thanks
1).
[self.view endEditing:YES];
2).you set the delegate method.
SOMEFIELD.text =delegate;
And .h file
#interface yourViewController : UIViewController<UITextFieldDelegate>
and in .m file you add the delegate method of the textfield
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
all you have to do is set someField's delegate your view controller and implement following method,
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

Cannot get DONE button on UITextField to work

I cannot seem to get the DONE button on the keyboard to dismiss the keyboard? Anyone know how to make it work?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
Your code, in particular resigning first responder of the textfield in -textFieldShouldReturn is fine.
I think you probably didn't hook things up correctly, possibly forgetting to set the delegate of your UITextField.
A snippet from a recent project of mine where I used a Next return key and a Done return key for two fields, toggling from the first to the second, and dismissing the second (which had a Done return key):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if(textField == self.nameTextField)
[self.descriptionTextField becomeFirstResponder];
return YES;
}
Concerning UITextView (your question specifically mentions UITextField in the title but your code has both for textfield and textview), you will need to resign first responder differently.
Since Text views are for longer text entry including multiple lines where you can also use the return key for a line break, you'll have to detect when the user changes the text.
If they try to enter a line break, then manually resign at that point.
Example follows:
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}

Dont show keyboard when textfield is tapped

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..

Go to next textfield when hit Return

I wrote a textFieldDone: method that's suppose to move the cursor to the next textfield when the Return button is tapped.
- (IBAction)textFieldDone:(id)sender {
[nextTextField becomeFirstResponder];
NSLog(#"in : textFieldDone");
}
I have connected the first textfield's "Did End On Exit" event to the File's Owner and chose the textFieldDone: method.
I also assigned the File's Owner as the textfield's delegate (because I need the view to scroll up/down accordingly so the keyboard won't hide the textfields).
When I run the app on the simulator and tap the return button the first textfield resign first responder and in the log I see that the program didn't go through the textFieldDone: method, but it did go through the textFieldDidEndEditing: method.
I used that method before and had no problem.
Is it because the File's Owner is the textfield's delegate?
You need to write on
- (BOOL) textFieldShouldReturn:(UITextField*) textField
to go to next text field.
Sample code:
-(BOOL) textFieldShouldReturn:(UITextField*) textField
{
if (textField == txt1)
{
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
if (textField == txt2)
{
[txt2 resignFirstResponder];
}
return YES;
}
Don't forget to add delegate UITextFieldDelegate to your UITextfield.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:txt1])
{
[txt2 becomeFirstResponder];
}
return true;
}
the above answers are correct, but to make this more general you should use the tag option
UITextField *txt1;
txt1.tag=1;
UITextField *txt2;
txt2.tag=2;
UITextField *txt3;
txt3.tag=3;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([[textField superview] viewWithTag:textField.tag+1])
{
[[[textField superview] viewWithTag:textField.tag+1] becomeFirstResponder];
}
else{ [textField resignFirstResponder];
}
return true;
}
note: don't use textField with tag 0. because all subViews have tag=0 by default.

Keyboard resignfirstresponder when Click Done

I have a textfield called searchBox and am trying to get rid of the keyboard if the user either clicks on Done.
Is there an IBAction that I need to know to do this?
You have to make your viewcontroller an UITextFieldDelegate and write in
viewDidLoad:
[searchBox setDelegate:self];
And then write:
- (BOOL)textFieldShouldReturn:(UITextField *)textfield
{
[searchbox resignFirstResponter];
return YES;
}
In your view controller:
- (void)viewDidLoad
{
// ...
searchBox.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[searchBox resignFirstResponder];
return YES;
}
See the UITextFieldDelegate documentation for details (you might want to return NO instead of YES in my example).