.m文件:
@interfaceRootViewController()<UISearchBarDelegate>
searchBar的位置和大小:
UISearchBar*searchBar1 = [[UISearchBaralloc]initWithFrame:CGRectMake(20,200,300,30)];
searchBar1.placeholder=@"请输入要搜索的内容";
[searchBar2setShowsCancelButton:YESanimated:YES];
searchBar1.delegate=self;
[self.viewaddSubview:searchBar1];
[searchBar1release];
-(void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar
{//一点击搜索框,就执行这个方法
NSLog(@点击搜索框");
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar
{
[searchBarsetShowsCancelButton:YESanimated:YES];
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
returnYES;
}
//实现实时搜索的方法
-(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];
}
网友评论