IOS Xcode Keyboard Button Function

I just can't get the Finish button to exit the keyboard.

I used this in the controller.h file

- (IBAction)textFieldDoneEditing:(id)sender; 

and this is for my controller.m file

 - (IBAction)textFieldDoneEditing:(id)sender { [sender resignFirstResponer]; } 

and I got mixed up with connecting the .xib part.

+7
source share
3 answers

Make the controller a delegate of UITextField / UITextView in IB or from code such as textField.delegate = self;

Editted: For this you need to declare the controller as a delegate of UITextFieldDelegate / UITextViewDelegate as

 @interface Controller : <UITextFieldDelegate> { ... 

then override the method:

 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } 

for UITextField and

 -(BOOL)textViewShouldEndEditing:(UITextView *)textView{ [textView resignFirstResponder]; return YES; } 

for UITextView

+23
source

In your .xib, right-click on the text view, drag it to "File Owner" and click "Delegate." Should it work now?

Edit: Oops, I'm sorry, I'm an idiot, do what the other guy says. If you do not know how to set a delegate to code, you can do it your own way in IB.

+1
source

Let me make my first contribution: If you have multiple text fields, group them in @property (strong, non-atomic)

*. H

 @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *collectingData; 

* m.

 -(BOOL)textFieldShouldReturn:(UITextField *)boxes { for (UITextField *boxes in collectingData) { [boxes resignFirstResponder]; } return YES; } 
0
source

All Articles