.m文件:
@interfaceRootViewController()<UISearchBarDelegate>
searchBar的位置和大小:
UISearchBar*searchBar1 = [[UISearchBaralloc]initWithFrame:CGRectMake(20,200,300,30)];
searchBar1.placeholder=@"請輸入要搜索的內(nèi)容";
[searchBar2setShowsCancelButton:YESanimated:YES];
searchBar1.delegate=self;
[self.viewaddSubview:searchBar1];
[searchBar1release];
-(void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar
{//一點擊搜索框,就執(zhí)行這個方法
NSLog(@點擊搜索框");
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar
{
[searchBarsetShowsCancelButton:YESanimated:YES];
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
returnYES;
}
//實現(xiàn)實時搜索的方法
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
NSLog(@"textDidChange:%@", searchBar.text);
}
//點擊搜索按鈕(這是點擊搜索按鈕之后再搜索)
-(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar
{
NSLog(@"搜索按鈕點擊丑念。击儡。蝙砌。");
NSLog(@"----%@", searchBar.text);
[searchBarresignFirstResponder];
}
//點擊取消按鈕(cancel)
-(void)searchBarCancelButtonClicked:(UISearchBar*)searchBar
{
NSLog(@"點擊取消按鈕拷邢。月培。谷暮。");
[selfsearchBar:searchBartextDidChange:nil];
[searchBarresignFirstResponder];
[searchBarsetShowsCancelButton:NOanimated:YES];
}