ios搜索栏 UISearchBarDelegate委托常用方法

请看下面的实例代码:


//点击键盘上的search按钮时调用

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    NSString *searchTerm = searchBar.text;

    [self handleSearchForTerm:searchTerm];

}


//输入文本实时更新时调用

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

    if (searchText.length == 0) {

        [self resetSearch];

        [table reloadData];

        return;

    }

    

    [self handleSearchForTerm:searchText];

}


//cancel按钮点击时调用

- (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

    isSearching = NO;

    search.text = @"";

    [self resetSearch];

    [table reloadData];

    [searchBar resignFirstResponder];

}


//点击搜索框时调用

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{

    isSearching = YES;

    [table reloadData];

}

UISearchBar上按钮的默认文字为Cancel,如果想改为其他文字请调用一下代码

for(id cc in [searchtext subviews]){  
   if([cc isKindOfClass:[UIButton class]]){  
       UIButton *btn = (UIButton *)cc;  
       [btn setTitle:@"取消"  forState:UIControlStateNormal];  
   }  
} 

comments powered by Disqus