以编程方式创建UISearchDisplayController [英] Creating a UISearchDisplayController programmatically
问题描述
我正在尝试以编程方式创建 UISearchDisplayController
。我有一个应该设置我的搜索控制器的方法,但是当我调用它时,没有任何反应。
I'm trying to create a UISearchDisplayController
programmatically. I have a method which should set up my search controller, but when I call it, nothing happens.
这是我的 -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 <中调用上述方法/ code>我的子类
UITableViewController
的方法。不幸的是,当我的表视图控制器显示在 UITabBarController
中时没有任何反应。
I call the above method in the -viewDidLoad
method of my subclassed UITableViewController
. Unfortunately nothing happens when my table view controller get's displayed in a UITabBarController
.
任何帮助都将不胜感激。
Any help would be greatly appreciated.
推荐答案
查看以下示例代码:
[ https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob /master/CoreDataTableViewController.m][1]
Check out the example code in:
[https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]
这里回复: https://github.com/JayMarshal/Grabcasts
它是coredatatableviewcontroller的扩展版本斯坦福iOS课程。
It is an expanded version of the coredatatableviewcontroller of the stanford iOS courses.
该代码的相关片段如下:
Relevant snippet of that code follows:
- (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)作为初始化的副作用。所以设置:
Basically it attaches the UISearchDisplayController to self (which must be a tableviewcontroller) as a side effect of the initialization. So setting:
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
而不是
myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
可以做到这一点。在调试中,检查myCon和self.searchDisplayController是否指向同一个对象?
Might do the trick. In debugging, check whether myCon and self.searchDisplayController are pointing to the same object?
更新:TVC的SDC属性中似乎有一个错误,它是不保留在runloop中。归档为: http://openradar.appspot.com/10254897 也在SO上提到,请参阅
UIViewController不保留其以编程方式创建的UISearchDisplayController
Updated: there seems to be a bug in the SDC property of the TVC that it is not retained in the runloop. Filed as: http://openradar.appspot.com/10254897 also mentioned on SO, see UIViewController does not retain its programmatically-created UISearchDisplayController
这篇关于以编程方式创建UISearchDisplayController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!