我正在努力创造一个 UISearchDisplayController
编程。我有一个应该设置我的搜索控制器的方法,但是当我调用它时,没有任何反应。
这是我的 -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];
}
我在上面调用了上面的方法 -viewDidLoad
我的子类的方法 UITableViewController
。不幸的是,当我的表视图控制器显示在一个时,没有任何反应 UITabBarController
。
任何帮助将不胜感激。
查看以下示例代码:
[https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]
在这里回购: https://github.com/JayMarshal/Grabcasts
它是stanford 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;
可能会做的伎俩。在调试中,检查myCon和self.searchDisplayController是否指向同一个对象?
更新:TVC的SDC属性中似乎存在一个错误,它不会保留在runloop中。归档为: http://openradar.appspot.com/10254897 也提到了SO,见
UIViewController不保留其以编程方式创建的UISearchDisplayController
查看以下示例代码:
[https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]
在这里回购: https://github.com/JayMarshal/Grabcasts
它是stanford 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;
可能会做的伎俩。在调试中,检查myCon和self.searchDisplayController是否指向同一个对象?
更新:TVC的SDC属性中似乎存在一个错误,它不会保留在runloop中。归档为: http://openradar.appspot.com/10254897 也提到了SO,见
UIViewController不保留其以编程方式创建的UISearchDisplayController