以编程方式创build一个UISearchDisplayController

我试图以编程方式创build一个UISearchDisplayController 。 我有一个方法,应该build立我的search控制器,但是当我打电话的时候,没有任何反应。

这我-setupSearch方法:

 - (void)setupSearch { UISearchBar *myBar; UISearchDisplayController *myCon; myBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; [myBar sizeToFit]; myCon = [[UISearchDisplayController alloc] initWithSearchBar:myBar contentsController:self]; [myBar release]; myCon.delegate = self; myCon.searchResultsDataSource = self; myCon.searchResultsDelegate = self; /* Setup scopes */ { NSMutableArray *scopes; NSUInteger count, i; NSString *aScope; count = SCOPE_COUNT; scopes = [[NSMutableArray alloc] initWithCapacity:count]; for(i = 0; i < count; i++) { // I create four scopes here } myCon.searchBar.scopeButtonTitles = scopes; [scopes release]; } [myCon release]; } 

我在我的子类UITableViewController-viewDidLoad方法中调用上述方法。 不幸的是,当我的表视图控制器得到显示在UITabBarController时,没有任何反应。

任何帮助将不胜感激。

查看下面的示例代码: [ https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m%5D [1 ]

在这里重申: https : //github.com/JayMarshal/Grabcasts

它是斯坦福iOS课程的coredatatableviewcontroller的扩展版本。

该代码的相关代码片段如下:

 - (void)createSearchBar { if (self.searchKey.length) { if (self.tableView && !self.tableView.tableHeaderView) { UISearchBar *searchBar = [[[UISearchBar alloc] init] autorelease]; self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; self.searchDisplayController.searchResultsDelegate = self; self.searchDisplayController.searchResultsDataSource = self; self.searchDisplayController.delegate = self; searchBar.frame = CGRectMake(0, 0, 0, 38); self.tableView.tableHeaderView = searchBar; } } else { self.tableView.tableHeaderView = nil; } 

基本上,它将UISearchDisplayController附加到self(它必须是一个tableviewcontroller),作为初始化的副作用。 所以设置:

 self.searchDisplayController.searchResultsDelegate = self; self.searchDisplayController.searchResultsDataSource = self; 

代替

 myCon.searchResultsDataSource = self; myCon.searchResultsDelegate = self; 

可能会诀窍。 在debugging中,检查myCon和self.searchDisplayController是否指向同一个对象?

更新:在TVC的SDC属性中似乎存在一个错误,它不保留在runloop中。 提起: http : //openradar.appspot.com/10254897也提到了所以,请参阅UIViewController不保留其编程创build的UISearchDisplayController