I have a DetailViewController, which implementation file contains this code:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController <UITextFieldDelegate>
{
__weak IBOutlet UITextField *nameField;
__weak IBOutlet UITextField *numberField;
}
#end
In my storyboard, I have set the ViewController's to DetailViewController and connected the delegate of both of my UITextFields to my DetailViewController. The implementation file of my DetailViewController contains this method to dismiss the keyboard when tapping somewhere other than the text field:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
This method is not called though, I have tested this using a breakpoint. What could be going wrong?
rdelmar is correct, the code you have only gets triggered when the user hits the "return" key on the keyboard, not when they click outside of the keyboard.
To get the behavior you are looking for, I'd add a Tap Gesture Recognizer to the view behind your text field, then put [nameField resignFirstResponder]; and [numberField resignFirstResponder]; in the tap gesture recognizer's code.
I created a sample project
added to the viewcontroller
added a textfield and connected its delegate
write the code in viewcontroller
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
it worked fine.Tried weak it also worked both as instance variable and property
So check if that you are reallocating it anywhere and also check its memory is same there using breakpoint
Why do you use _weak? Remove it.
In your .m file do (in viewDidLoad):
[nameField setDelegate:self];
Same for the other textfield(s).
Related
I want to make the keyboard disappear when the user clicks the "return" button, I was told to use
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[tf resignFirstResponder];
return YES;
}
But nothing happens when I click the "return" button, the method isn't even being called. I am doing this in
#interface gameOverMenu : UIView
not in the ViewController. I also don't use interface builder. What should I do?
You need to make sure you implement the UITextFieldDelegate and set your UITextField delegate to self. In your .h file:
#interface gameOverMenu : UIView <UITextFieldDelegate>
And somewhere in your .m file (viewDidLoad: maybe):
self.yourTextField.delegate = self;
Now your -(BOOL)textFieldShouldReturn:(UITextField *)textField method should be called.
Make sure that you have set the parent class (whatever it is) as a UITextFieldDelegate
I just can't make the "Done" button to quit the keyboard.
I used this in my controller.h file
- (IBAction)textFieldDoneEditing:(id)sender;
and this for my controller.m file
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponer];
}
and I'm mixed up in wiring the .xib part.
Make the controller a delegate of the UITextField/UITextView in IB or from code like textField.delegate = self;
Editted:
For this you need to declare the controller 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
In your .xib, right click on your text view, drag to "File's Owner", and click "delegate". Should work now?
Edit: Whoops, sorry I'm an idiot, do what that other guy says. If you don't know how to set the delegate in code though, you can do it my way in IB.
Let me make my first contribution:
If you have multiple text fields, group them in a #property (strong, nonatomic)
*.h
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *collectingData;
*.m
-(BOOL)textFieldShouldReturn:(UITextField *)boxes
{
for (UITextField *boxes in collectingData) {
[boxes resignFirstResponder];
}
return YES;
}
I have UITextField and I have
#property (nonatomic, assign) id <UITextFieldDelegate> delegate;
After pressing a done button on keybord, it should dismiss. Can someone tell how I can do it using this property?
The dismissing of the keyboard does not happen merely by being a delegate of the UITextField. Your delegate must dismiss it.
In the viewDidLoad in your controller you must assign the delegate, or set it up in Interface Builder:
- (void)viewDidLoad {
self.textField.delegate = self;
}
Then in your controller, implement the following delegate method.
#pragma mark - UITextFieldDelegate
// This method gets called when you hit the enter key on the keyboard,
// or in this case 'DONE'. The textfield is asking if it should put
// a carriage return in the field. This is our opportunity to dismiss
// the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder]; // This is what hides the keyboard
return NO;
}
Further reading:
Responder Chain - developer.apple.com
UITextFieldDelegate Protocol Reference - developer.apple.com
Link didEndOnExit to a method that has [self resignFirstResponder]
I have a viewController with this:
#interface RootViewController : UIViewController <UITextFieldDelegate>{
Then I have a textfield added programmatically like this in viewController.m:
UITextField*textFieldRounded=[[UITextField alloc] initWithFrame:frame];
textFieldRounded.delegate = self;
In the same .m i have tried all of these:
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(#"returned?");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(#"returned?");
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(#"returned?");
return YES;
}
The keyboard appears fine when I click the text field, which is in a subview of the viewController. but pressing Done does nothing. The NSLog statements never appear, so these methods are never getting called. Any ideas?
I just barely had this question, maybe, but what I did was attached a tap gesture recognizer object to the view, made a instance method called clear keyboard:, and inside the function I put [whateverItIsThatOpenedTheKeyboard resignFirstResponder]; and that's it. Maybe you asking something totally different, but I thought I would try and help :)
Declare the UITextField in .h file as an IBOutlet with property and synthesize it in .m file.
Use this delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
Add this delegate method to your code and run your app once again..
I'm using iOS 5 and trying to use TextField with UITextFieldDelegate, it's worked exactly like I want (but JUST in the first View). I don't understand, why it's not working in the next view.
For simple example, I created new project (ViewController). There I added one button, that connect to another view (SecondViewController). In the SecondViewController, I have one TextField. With this textField I want to use textFieldShouldReturn. But it seems, that this method is not being called. What I know, I should write the delegate in ViewDidLoad. Should I write myTextField.delegate = self; ? but I think something wrong there. I used Debugging, and always at that position, i'm getting problem. Could you please tell me, what the problem is? and how can i solve it ;(
Thanks in advance.....
Here is my code (that it works, in the first view (ViewController). Unfortunately here not (SecondViewController):
SecondViewController.h
#interface SecondViewController : UIViewController<UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *myTextField;
#end
SecondViewController.m
#implementation SecondViewController
#synthesize myTextField;
- (void)viewDidLoad{
[super viewDidLoad];
myTextField.delegate = self; // here i get the problem
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{ // this method is not being called
[textField resignFirstResponder];
NSLog(#"is called!");
NSString *valueMyTextField = myTextField.text;
NSLog(#"%#", valueMyTextField);
return YES;
}
Problem solved... :)
The problem was the connection from firstView to secondView.
Do not use addSubView, if you want to add Controller!
Use presentModalViewController :)
Hope it helps, in case you have the same problem like me.
In the nib file, please check that whether you have checked the Auto-enable Return Key check box for the text field.