我想在用户完成输入后搜索一些单词, 我已经尝试过这个选择器了,但是每次输入都会调用它。
#pragma mark textFieldValueChanged
- (void)inputSearchTextChanged:(UITextField *)textField
{
if (textField.text.length == 0) {
self.searchView.tipsView.hidden = NO;
} else {
self.searchView.tipsView.hidden = YES;
}
if (self.searchView.lySelectView.selectState == LYTabSelectOptionLeft) {
[self searchTagWithTagTitle:textField.text];
} else {
[self searchPeopleWithTagTitle:textField.text];
}
}
答案 0 :(得分:1)
尝试
-(void) textFieldDidEndEditing:(UITextFiled*)textfield) {
}
答案 1 :(得分:0)
set delegate of textfield and try below delegate method
-(void) textFieldDidEndEditing:(UITextFiled*)textfield)
{
if (textField.text.length == 0) {
self.searchView.tipsView.hidden = NO;
} else {
self.searchView.tipsView.hidden = YES;
if (self.searchView.lySelectView.selectState == LYTabSelectOptionLeft) {
[self searchTagWithTagTitle:textField.text];
} else {
[self searchPeopleWithTagTitle:textField.text];
}
}
答案 2 :(得分:0)
您可以使用UITextFieldDelegate协议。更多细节here。您必须在ViewController中实现方法 - (void)textFieldDidEndEditing:(UITextFiled )textfield)*。
-(void) textFieldDidEndEditing:(UITextFiled*)textfield)
{
if (textField.text.length == 0) {
self.searchView.tipsView.hidden = NO;
} else {
self.searchView.tipsView.hidden = YES;
if (self.searchView.lySelectView.selectState == LYTabSelectOptionLeft) {
[self searchTagWithTagTitle:textField.text];
} else {
[self searchPeopleWithTagTitle:textField.text];
}
}
将您的ViewController分配为委托:
self.textField.delegate = self; //self is the ViewController.
稍后在澄清之后进行修改:
当用户停止输入时,您需要一个事件:您可以使用计时器模拟此事件。
@property (strong, nonatomic) NSTimer *timer;
....
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)strin {
if (self.timer != nil) {
[self.timer invalidate];
self.timer = nil;
}
self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(eventNeeded) userInfo:nil repeats:NO];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];
return YES;
}
-(void) eventNeeded{
NSLog(@"User has stopped typing.");
}