How to resign first responder from text field when

2019-01-22 07:04发布

I have filled my view with ScrollView (same size as the view) and I'm stuck at how to resign first responder when user tap elsewhere in the View (or the scrollview). Any idea on how to do that ? I'm using the following method but it's not working as expected:

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

Thx for helping,

Stephane

17条回答
【Aperson】
2楼-- · 2019-01-22 07:36

For a more robust and clean solution add a tap gesture recognizer to your primary view.

This will work better with nested views and will be cleaner than secret buttons in code and UI builder.

In your view did load:

UITapGestureRecognizer* tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[tapBackground setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapBackground];

..and define your target action to be triggered on tap:

-(void) dismissKeyboard:(id)sender
{
    [self.view endEditing:YES];
}
查看更多
萌系小妹纸
3楼-- · 2019-01-22 07:37

I found an answer here: link

If your ultimate aim is just to resign the first responder, this should work: [self.view endEditing:YES]

This method is designed right for it! Reference: endEditing:

Causes the view (or one of its embedded text fields) to resign the first responder status.

查看更多
做自己的国王
4楼-- · 2019-01-22 07:37

Update

I found another simple way

simply declare a property :-

@property( strong , nonatomic) UITextfield *currentTextfield;

and a Tap Gesture Gecognizer:-

@property (strong , nonatomic) UITapGestureRecognizer *resignTextField;

In ViewDidLoad

_currentTextfield=[[UITextField alloc]init];
_resignTextField=[[UITapGestureRecognizer alloc]initWithTarget:@selector(tapMethod:)];

[self.view addGestureRecognizer:_resignTextField];

Implement the textfield delegate method didBeginEditing

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


      _currentTextfield=textField;

    }

Implement Your Tap Gesture Method (_resignTextField)

 -(void)tapMethod:(UITapGestureRecognizer *)Gesture{

     [_currentTextfield resignFirstResponder];

 }
查看更多
Lonely孤独者°
5楼-- · 2019-01-22 07:38

UIViewController inherits from UIResponder so a naive way (I am pretty sure is not the smartest way to do it) would be by overwriting the following methods (at least 1 of them)

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
- touchesCancelled:withEvent:

Next you could get the touched view by doing

UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];

finally resign the first responder if that view is not your text field

if(touchedView != textField){
    [textField resignFirstResponder];
}

_

Demerits of this approach:

You will have to handle the "tap" by yourself. (Same problem as the old iOS 3.1 or earlier). You will have to come up with your own implementation to differentiate single taps from drags, swipes, double taps, long taps, etc. Is not hard to get it working well but it is not likely you get it exactly the same way Apple detects taps (timings, distances, thresholds count!) However, that depends on your needs.

If your view structure is simple enough then you could add a gesture recognizer to the container view and resign the first responder every time the handler is called :)

Hope this helps

查看更多
来,给爷笑一个
6楼-- · 2019-01-22 07:39

Give a unique Tag to your UITextfield (i.e. 333), then to resign the first responder do this:

UITextField *textField = (UITextField *)[self.view viewWithTag:333];
[textField resignFirstResponder];
查看更多
淡お忘
7楼-- · 2019-01-22 07:40

For my implementation, [self.view endEditing:YES] did not work.

To finally make sure the keyboard was hidden, I had to make one of my views the first responder, and then immediately make it resign as per the function below:

-(void) hideKeyboardPlease{
   [uiTextTo becomeFirstResponder];
   [uiTextTo resignFirstResponder];
}
查看更多
登录 后发表回答