以编程方式创建 UISearchDisplayController
我正在尝试以编程方式创建一个 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];
}
我在子类 UITableViewController
的 -viewDidLoad
方法中调用上述方法。不幸的是,当我的表视图控制器显示在 UITabBarController
中时,什么也没有发生。
任何帮助将不胜感激。
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.
This my -setupSearch
method:
- (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
.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看示例代码:
[https:// github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]仓库:https://github.com/JayMarshal/Grabcasts
是stanford iOS课程的coredatatableviewcontroller的扩展版本。
该代码的相关片段如下:
基本上,它将 UISearchDisplayController 附加到 self(必须是 tableviewcontroller),作为初始化的副作用。所以设置:
而不是
Might 可以解决问题。在调试时,检查 myCon 和 self.searchDisplayController 是否指向同一个对象?
更新:TVC 的 SDC 属性似乎有一个错误,它没有保留在运行循环中。提交为:http://openradar.appspot.com/10254897 也提到过,请参阅
UIViewController 不保留其以编程方式创建的 UISearchDisplayController
Check out the example code in:
[https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]Repo here: https://github.com/JayMarshal/Grabcasts
It is an expanded version of the coredatatableviewcontroller of the stanford iOS courses.
Relevant snippet of that code follows:
Basically it attaches the UISearchDisplayController to self (which must be a tableviewcontroller) as a side effect of the initialization. So setting:
Instead of
Might do the trick. In debugging, check whether myCon and self.searchDisplayController are pointing to the same object?
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