Reputation: 21
I have four UITextFields and associated each with an outlet in .h files. I have also defined 4 dismiss function for each textfield as shown below:
- (IBAction)dismiss1:(id)sender;
- (IBAction)dismiss2:(id)sender;
- (IBAction)dismiss3:(id)sender;
- (IBAction)dismiss4:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *weight;
@property (strong, nonatomic) IBOutlet UITextField *age;
Implementation of dismiss function in the .m file:
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss2:(id)sender {
[sender resignFirstResponder];
}
...
I am very sure that the outlet is connected to each UITextFiled correctly. The IBAction of each dismiss function is also connected with 'Editing did end' event correspondingly. However, when I ran the app using simulator, the keyboard will not dismiss when I click 'Enter/Done'. It's also very weird that when I place the breakpoint inside the dismiss function, clicking 'Enter' when typing in the corresponding UITextField does not bring up the debugger.
Thanks a lot for helping!
Update: I checked the object type of sender (dismiss1) using breakpoint and it's UITextFiled. However, I did not entered the debugging mode when I click 'Enter' in the first TextField, but entered the debugging mode when I click on the second TextField (before typing).
Upvotes: 1
Views: 1624
Reputation: 139
The Editing did end
is called when a textfield did lose the "focus", mean it is not the first responder any more (because it has resigned or an other one became first responder).
Use textFieldShouldReturn
to detect when the return key is called.
And call resignFirstResponder
on the texfield when this is called. The Editing did end
event will occur after that.
Edit : you have to implement something (Your ViewController?) as delegate of the textfield.
Upvotes: 0
Reputation: 675
Assign the tag to different textfields then >
textfield.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textfield.tag==num)
{
[textField resignFirstResponder];
}
return YES;
}
Upvotes: 0
Reputation: 243
set UITextFielDelegate
textfieldname.delegate = self;
use this code it will resolve your issue
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Upvotes: 1
Reputation: 1965
No need to create four actions for textfield
You can set delegate of Textfield and use following method
- (void)textFieldDidEndEditing:(UITextField *)textField
Inside this method you can compare your textfield with method returning textfield and get you task done in "if" condition
Hope it help you!!
Upvotes: 0
Reputation: 2207
Add text field delegate
name.delegate = self;
email.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Upvotes: 1
Reputation: 23407
Use the code to resign keyBoard without specify textFiled name.
- (IBAction)dismiss1:(UITextField*)sender {
[self.view endEditing:true];
// or use [sender resignFirstResponder];
}
Upvotes: 1