TextFieldShouldReturn not working (using storyboard on Xcode 4)

I have a button and a text box. I just want the keyboard to disappear when I press a button. Why is my code below not working.

Update: I saw something about the file owner. I do not understand how to do this in Xcode4. I am using a storyboard and I do not see the file icon.

Update 2: I found tut http://www.techotopia.com/index.php/Writing_iOS_4_Code_to_Hide_the_iPhone_Keyboard_%28Xcode_4%29 , but it uses the XIB file on Xcode 4, not the storyboard. How to do this using a storyboard?

myViewController.h

@interface myViewController : UIViewController <UITextFieldDelegate> { UITextField *myTextField; } @property (retain, nonatomic) IBOutlet UITextField *myTextField; 

myViewController.m

 - (BOOL)textFieldShouldReturn: (UITextField *)textField { [textField resignFirstResponder]; return YES; } - (void)viewDidLoad { [super viewDidLoad]; myTextField.delegate = self; } - (IBAction)DoCalc:(id)sender { // ... } 
+4
source share
4 answers

Make sure that the textFieldShouldReturn function that you wrote in myViewController.m is even called. Set a breakpoint in it, and then run the simulator. Press the back key on the keyboard. If the program does not break, then the function you wrote is not called.

If so, it is because you have not delegated UIResponder instructions to your view controller. Make sure you delegate the responsibilities of the responder to your myViewController class for the UITextField that you are working with.

In the storyboard, you do this by controlling the drag from the UITextField widget to the yellow frame under the scene and selecting โ€œdelegateโ€ from the pop-up menu that appears.

+5
source

textFieldShouldReturn: should return NO to hide the keyboard. One more thing: do not set the first responder to self , [textField resignFirstResponder] enough, iOS should figure nextResponder .

+3
source

You can hide the key-key in this method ...

 - (void)textFieldDidEndEditing:(UITextField *)textField { [textField resignFirstResponder]; } 
+1
source

Delete [self becomeFirstResponder]; must do it.

resignFirstResponder is a keyboard rejection. and becomeFirstResponder - open the keyboard. so in your code you close and then open the keyboard again. The last action is opened by the keyboard, so it will not be closed.

you can look at the UIResponder class as a superclass of UITextField

+1
source

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


All Articles