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
{

}

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

(BOOL)searchDisplayController:(UISearchDisplayController )controller shouldReloadTableForSearchString:(NSString )searchString
{
return YES;
}
// 当Scope Bar选择发送变化时候,向表视图数据源发出重新加载消息

(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{

return YES;
}这三个方法 我输入文字后 断点后发现一个都没执行 为什么啊 第一次写搜索框

你把设置成全局变量试试

searchDisplayController 这个的delegate没有设

UISearchDisplayDelegate 这个的代理你没有设啊

要在当前的视图控制器中声明一个UISearchDisplayController的实例,然后这个实例对象也要实现一个代理,该代理的方法和tableView的代理方法是一样的。

var searchDisplay: UISearchDisplayController?
searchDisplay = UISearchDisplayController(searchBar: searchBar, contentsController: self)
searchDisplay!.seearchResultsDataSource = self;
searchDisplay!.searchRestultsDelegate = self;

并且让当前视图遵守UISearchDisplayDelegate,实现方法searchDisplayController