我正在尝试以编程方式创建一个 UISearchDisplayController
。我有一个方法应该设置我的搜索 Controller ,但是当我调用它时,没有任何反应。
这是我的 -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
方法中调用上述方法。不幸的是,当我的 TableView Controller 显示在 UITabBarController
中时,什么也没有发生。
如有任何帮助,我们将不胜感激。
最佳答案
查看示例代码: <罢工>[ https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1] 罢工>
此处 repo :https://github.com/JayMarshal/Grabcasts
是stanford iOS类(class)的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 附加到自身(它必须是一个 tableviewcontroller)作为初始化的副作用。所以设置:
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
代替
myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
可能会成功。调试时检查myCon和self.searchDisplayController是否指向同一个对象?
更新:TVC 的 SDC 属性似乎有一个错误,它没有保留在运行循环中。归档为:http://openradar.appspot.com/10254897在 SO 上也提到了,请参阅 UIViewController does not retain its programmatically-created UISearchDisplayController
关于ios - 以编程方式创建 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696260/