ios - 以编程方式创建 UISearchDisplayController

标签 ios uisearchdisplaycontroller

我正在尝试以编程方式创建一个 UISearchDisplayController。我有一个方法应该设置我的搜索 Controller ,但是当我调用它时,没有任何反应。

这是我的 -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 方法中调用上述方法。不幸的是,当我的 TableView Controller 显示在 UITabBarController 中时,什么也没有发生。

如有任何帮助,我们将不胜感激。

最佳答案

查看示例代码: <罢工>[ https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]

此处 repo :https://github.com/JayMarshal/Grabcasts

是stanford iOS类(class)的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 附加到自身(它必须是一个 tableviewcontroller)作为初始化的副作用。所以设置:

self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;

代替

myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;

可能会成功。调试时检查myCon和self.searchDisplayController是否指向同一个对象?

更新:TVC 的 SDC 属性似乎有一个错误,它没有保留在运行循环中。归档为:http://openradar.appspot.com/10254897在 SO 上也提到了,请参阅 UIViewController does not retain its programmatically-created UISearchDisplayController

关于ios - 以编程方式创建 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696260/

相关文章:

iphone - 如何使用当前 View 信息自定义 UINavigationController 的顶部栏?

iphone - 如何使带有 UIPageViewController 点的底部栏半透明?

ios - 使用 UIRefreshControl 在 UITableView 中禁用底部反弹

iphone - 访问 JSON 文件以填充 UIPickerView

objective-c - 自定义 UITableViewCell 中的属性未针对 UISearchDisplayController 的表进行初始化

ios - 在 UIViewController 中添加搜索显示 Controller

ios - 让线程进入休眠状态但保持动画继续 - 这可能吗?

ios - 搜索显示 Controller 结果 TableView 无法在 iOS 6 中滚动

ios - 如何在数组中创建一个数组,然后检查它是否包含字符串。 ( swift )

iphone - 返回结果时搜索显示 Controller 崩溃