ios 以编程方式创建 UISearchDisplayController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8696260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a UISearchDisplayController programmatically
提问by v1Axvw
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.
我正在尝试以UISearchDisplayController
编程方式创建一个。我有一个方法应该设置我的搜索控制器,但是当我调用它时,没有任何反应。
This my -setupSearch
method:
这是我的-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];
}
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
.
我在-viewDidLoad
我的子类的方法中调用上述方法UITableViewController
。不幸的是,当我的表视图控制器 get 显示在UITabBarController
.
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by Bjinse
Check out the example code in:
[https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]
查看以下示例代码:
[ https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]
Repo here: https://github.com/JayMarshal/Grabcasts
此处回购:https: //github.com/JayMarshal/Grabcasts
It is an expanded version of the coredatatableviewcontroller of the stanford iOS courses.
它是stanford iOS课程的coredatatableviewcontroller的扩展版本。
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;
}
Basically it attaches the UISearchDisplayController to self (which must be a tableviewcontroller) as a side effect of the initialization. So setting:
基本上它将 UISearchDisplayController 附加到 self (必须是 tableviewcontroller)作为初始化的副作用。所以设置:
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
Instead of
代替
myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
Might do the trick. In debugging, check whether myCon and self.searchDisplayController are pointing to the same object?
可能会成功。在调试时,检查 myCon 和 self.searchDisplayController 是否指向同一个对象?
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/10254897also mentioned on SO, see UIViewController does not retain its programmatically-created UISearchDisplayController
更新:TVC 的 SDC 属性中似乎有一个错误,它没有保留在 runloop 中。归档为:http: //openradar.appspot.com/10254897也提到了 SO,请参阅 UIViewController 不保留其以编程方式创建的 UISearchDisplayController