以编程方式创建 UISearchDisplayController

发布于 2024-12-24 18:05:58 字数 1157 浏览 0 评论 0原文

我正在尝试以编程方式创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-12-31 18:05:58

查看示例代码:
[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;

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:

- (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:

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?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文