问题 以编程方式创建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];
}

我在上面调用了上面的方法 -viewDidLoad 我的子类的方法 UITableViewController。不幸的是,当我的表视图控制器显示在一个时,没有任何反应 UITabBarController

任何帮助将不胜感激。


2992
2018-01-01 23:18


起源



答案:


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

可能会做的伎俩。在调试中,检查myCon和self.searchDisplayController是否指向同一个对象?

更新:TVC的SDC属性中似乎存在一个错误,它不会保留在runloop中。归档为: http://openradar.appspot.com/10254897 也提到了SO,见 UIViewController不保留其以编程方式创建的UISearchDisplayController


12
2018-01-20 18:54



从doc,on contentsController:“这通常是UITableViewController的一个实例。”通常,不需要。 - Jacob Jennings
所以,如果你不想使用 UITableViewController,你可以简单地拥有一个 UIViewController 符合协议 UITableViewDataSource 和 UITableViewDelegate。 - Matt Quiros
我的问题是 self.searchDisplayController 是一个只读属性 UITableViewController,但这里的代码是设置它。 - Tyler Cloutier
该属性在CoreDataTableViewController.h中被重新声明为读/写。所提到的repo似乎不再包含此源文件。 - Bjinse
当你初始化searchDisplayController(并将其设置为强属性,保留它)时,它会自动设置你传递给它的initWithSearchBar ...方法的UIViewController的searchDisplayController。 - GreatWiz


答案:


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

可能会做的伎俩。在调试中,检查myCon和self.searchDisplayController是否指向同一个对象?

更新:TVC的SDC属性中似乎存在一个错误,它不会保留在runloop中。归档为: http://openradar.appspot.com/10254897 也提到了SO,见 UIViewController不保留其以编程方式创建的UISearchDisplayController


12
2018-01-20 18:54



从doc,on contentsController:“这通常是UITableViewController的一个实例。”通常,不需要。 - Jacob Jennings
所以,如果你不想使用 UITableViewController,你可以简单地拥有一个 UIViewController 符合协议 UITableViewDataSource 和 UITableViewDelegate。 - Matt Quiros
我的问题是 self.searchDisplayController 是一个只读属性 UITableViewController,但这里的代码是设置它。 - Tyler Cloutier
该属性在CoreDataTableViewController.h中被重新声明为读/写。所提到的repo似乎不再包含此源文件。 - Bjinse
当你初始化searchDisplayController(并将其设置为强属性,保留它)时,它会自动设置你传递给它的initWithSearchBar ...方法的UIViewController的searchDisplayController。 - GreatWiz