self.searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
self.searchbar.delegate = self;
UITextField *field = (UITextField *)[[[self.searchbar.subviews firstObject] subviews] lastObject];
field.clearButtonMode = UITextFieldViewModeWhileEditing;
field.returnKeyType = UIReturnKeyDone;
[self.view addSubview:self.searchbar];
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
[self.searchbar resignFirstResponder];
}
}
我确定搜索栏代表已经离开;如果我逐个删除字符,它将resignFirstResponder
,但如果我按下符号' x',它也会出现在此方法中,但它不会resignFirstResponder
。为什么会这样?