Resigning First Responder for multiple UITextField

2019-03-15 06:18发布

There is an application in which I am generating multiple UITextFields dynamically. I want to resign first responder whenever the UITextFields are not selected (touch outside the UITextField). How can I know that of which UITextField I have to resign first responder? Please specify any other way beyond the 'tag' concept because I have tried that. Please suggest the right direction. Thanks in advance.

13条回答
聊天终结者
2楼-- · 2019-03-15 06:57

You can set textFields as properties and synthesize them. You will need as many properties as you are using in your app.

You can have @synthesise myTextField0, myTextField1, myTextField2; for three textFields. Just assign each UITextField you are using to these properties, while declaring them.

And when you want to resign them, just use [self.myTextField0 resignFirstResponder] at textFieldDidEndEditing or which ever function you want to resign them. And you can use this for other textFields also. This is the way to handle multiple textFields

In general, you can skip all these steps, with textField.returnKeyType = UIReturnKeyDone; if you have a DONE return key, you can just go to the method

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

or you can use tags to tag certain textFields and then resign particular ones.

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-15 06:58
**[self.view endEditing:TRUE];** //Resign firstresponder for all textboxes on the view
查看更多
欢心
4楼-- · 2019-03-15 06:58

Using [self.view endEditing:YES]; to resignFirstResponder for the current active UITextField.

查看更多
乱世女痞
5楼-- · 2019-03-15 06:58

You can use endEditing instead of resignFirstResponder

Try This

[self.view EndEditing:YES]
查看更多
闹够了就滚
6楼-- · 2019-03-15 07:00

By the way..i have done this in different manner.. Not looking quite a good programming Champ tech but enough to solve my work!!

for(UIView *v in self.view.subviews)
    {
        if(([v isMemberOfClass:[UITextField class]]==YES) && !(CGRectContainsPoint(v.frame,[[touches anyObject] locationInView:self.View)))
           {
               v.userInteractionEnabled=NO;
               if([v isEditing])
               {
               [v resignFirstResponder];
               }
           }
    }
查看更多
再贱就再见
7楼-- · 2019-03-15 07:04

You can implement delegate method

- (void)textFieldDidBeginEditing:(UITextField *)textField; 

in that you can take currentTextField = textField;

in another delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField; 

you can do currentTextField = nil;

you can then resign currentTextField....

查看更多
登录 后发表回答