【问题标题】:Creating a UISearchDisplayController programmatically以编程方式创建 UISearchDisplayController
【发布时间】:2012-01-01 23:18:59
【问题描述】:

我正在尝试以编程方式创建 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 中时,什么也没有发生。

任何帮助将不胜感激。

【问题讨论】:

    标签: ios uisearchdisplaycontroller


    【解决方案1】:

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

    在这里回购:https://github.com/JayMarshal/Grabcasts

    它是斯坦福 iOS 课程的 coredatatableviewcontroller 的扩展版本。

    该代码的相关sn-p如下:

    - (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 属性中似乎有一个错误,它没有保留在运行循环中。归档为:http://openradar.appspot.com/10254897 在 SO 上也提到过,请参阅 UIViewController does not retain its programmatically-created UISearchDisplayController

    【讨论】:

    • 来自文档,在 contentsController 上:“这通常是 UITableViewController 的一个实例。”。通常,不需要。
    • 因此,如果您不想使用UITableViewController,您可以简单地使用符合UITableViewDataSourceUITableViewDelegate 协议的UIViewController
    • 我的问题是self.searchDisplayControllerUITableViewController 上的只读属性,但这里的代码正在设置它。
    • 该属性在 CoreDataTableViewController.h 中被重新声明为读/写,但提到的 repo 似乎不再包含此源文件。
    • 当您初始化 searchDisplayController(并将其设置为强属性以保留它)时,它会自动设置您传递给它的 initWithSearchBar... 方法的 UIViewController 的 searchDisplayController。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2013-07-25
    • 2013-07-22
    • 2018-06-04
    相关资源
    最近更新 更多