Reputation: 31647
I am trying to implement search bar in one of my page.
I am not using regular search bar due to its design.
What I have is as below.
UIImageView above UIView (textfield background)
UITextField above UIImageView (textfield)
I am using delegates
for UITextField
.
In code I have searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
to show the clear button.
Search is working fine but the problem is in delegate of clear button.
I have below code
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
NSLog(@"clicked clear button");
[textField resignFirstResponder]; // this is not working
// also below is not working
// [searchTF resignFirstResponder];
}
return YES;
}
When I click clear button, I get NSLog of text "clicked clear button", however the keyboard doesn't get dismissed.
Any idea why keyboard is not getting dismissed when I have
Even I tried as below using [self.view endEditing:YES];
, but still its not working.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[self.view endEditing:YES];
[self hideAllKeyboards];
}
return YES;
}
Upvotes: 3
Views: 2914
Reputation: 3275
Additional to my comment I made some testing and here are my results:
I've just implemented a UITextField
with all delegate methods like this:
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(@"Should Clear");
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(@"Begin editing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"End editing");
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(@"Should begin editing");
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"Should end editing");
return YES;
}
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSLog(@"Change char");
return YES;
}
As soon as you hit the clear button the log outputs:
2014-07-26 11:08:44.558 Test[36330:60b] Should Clear
2014-07-26 11:08:44.558 Test[36330:60b] Should end editing
2014-07-26 11:08:44.559 Test[36330:60b] End editing
2014-07-26 11:08:44.560 Test[36330:60b] Should begin editing
2014-07-26 11:08:44.561 Test[36330:60b] Begin editing
As you can see the shouldBeginEditing
and the didBeginEditing
methods get called after clearing so the resignFirstResponder
in textFieldshouldClear
gets called just before a new becomeFirstResponder
is called by shouldBeginEditing
or didBeginEditing
.
Upvotes: 5
Reputation: 31647
I am not sure what was the problem, but I solved calling the dismiss keyboard method after some interval using NSTimer
.
Below is what I did.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(hideAllKeyboards) userInfo:nil repeats:NO];
}
return YES;
}
-(void) hideAllKeyboards {
[searchTF resignFirstResponder];
}
This way, after clicking the clear button, keyboard is getting dismissed.
It would be great if someone post answer as why this is happening. I will mark that answer as accepted.
As per Daniel answer, I did below.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
isFilterOn.text = @"no";
[textField resignFirstResponder];
textField.text = @"";
[self myTextFieldDidChange];
}
return NO;
}
In myTextFieldDidChange
, I am showing the filtered list and un-filtered list based on the text I have in UITextField
.
Upvotes: 0
Reputation: 3162
I believe your textfield IBOutlet is properly connected ,Although you can try
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Upvotes: 0