以编程方式创建UISearchDisplayController

v1A*_*xvw 11 uisearchdisplaycontroller ios

我正在尝试以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];
}
Run Code Online (Sandbox Code Playgroud)

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

任何帮助将不胜感激.

Bji*_*nse 13

查看以下示例代码: [ 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;
}
Run Code Online (Sandbox Code Playgroud)

基本上它将UISearchDisplayController附加到self(必须是tableviewcontroller)作为初始化的副作用.所以设置:

self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
Run Code Online (Sandbox Code Playgroud)

代替

myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
Run Code Online (Sandbox Code Playgroud)

可以做到这一点.在调试中,检查myCon和self.searchDisplayController是否指向同一个对象?

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

  • 我的问题是`self.searchDisplayController`是`UITableViewController`的readonly属性,但这里的代码是设置它. (7认同)
  • 当你初始化searchDisplayController(并将其设置为强属性,保留它)时,它会自动设置你传递给它的initWithSearchBar ...方法的UIViewController的searchDisplayController. (4认同)
  • 从doc,on contentsController:"这通常是UITableViewController的一个实例." 通常,不是必需的. (2认同)