iOS 为什么UISearchBar的协议方法始终不执行 如下代码

<UITableViewDelegate, UITableViewDataSource,BookDetailViewDelegate,UISearchBarDelegate, UISearchDisplayDelegate>这是协议

这是初始化
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44)];

_searchBar.delegate = self;
[_searchBar setShowsScopeBar:NO];
[_searchBar sizeToFit];

[self.view insertSubview:_searchBar atIndex:3];

都在.m文件中

  • (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {

    //查询所有
    [self filterContentForSearchText:@"" scope:-1];

    }

//当文本内容发生改变时候,向表视图数据源发出重新加载消息

  • (BOOL)searchDisplayController:(UISearchDisplayController )controller shouldReloadTableForSearchString:(NSString )searchString
    {
    [self filterContentForSearchText:searchString scope:_searchBar.selectedScopeButtonIndex];
    //YES情况下表视图可以重新加载
    return YES;
    }

// 当Scope Bar选择发送变化时候,向表视图数据源发出重新加载消息

  • (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
    {
    [self filterContentForSearchText:_searchBar.text scope:searchOption];
    // YES情况下表视图可以重新加载
    return YES;
    }这三个方法 我输入文字后 断点后发现一个都没执行 为什么啊 第一次写搜索框

阅读 3.4k
1 个回答

delegate 用错了,是 UISearchBar 的 delegate ,不是 UISearchDisplayController 的,你没有用到 SearchDisplayController ,

PS: 下次提问能好好排下版吗……

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题