美文网首页架构师之路
iOS开发--搜索功能

iOS开发--搜索功能

作者: XDRS潇潇雨歇 | 来源:发表于2016-02-20 00:17 被阅读0次

    .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];

    }

    相关文章

      网友评论

        本文标题:iOS开发--搜索功能

        本文链接:https://www.haomeiwen.com/subject/mojvkttx.html