Ios 以编程方式创建UISearchDisplayController

Ios 以编程方式创建UISearchDisplayController,ios,uisearchdisplaycontroller,Ios,Uisearchdisplaycontroller,我正在尝试以编程方式创建一个UISearchDisplayController。我有一个应该设置搜索控制器的方法,但是当我调用它时,什么也没有发生 这是我的-setupSearch方法: - (void)setupSearch { UISearchBar *myBar; UISearchDisplayController *myCon; myBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; [myBar

我正在尝试以编程方式创建一个
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];
}
-(无效)设置搜索{
UISearchBar*myBar;
UISearchDisplayController*myCon;
myBar=[[UISearchBar alloc]initWithFrame:CGRectZero];
[myBar sizeToFit];
myCon=[[UISearchDisplayController alloc]
initWithSearchBar:myBar内容控制器:self];
[myBar释放];
myCon.delegate=self;
myCon.searchResultsDataSource=self;
myCon.searchResultsDelegate=self;
/*设置范围*/
{
NSMutableArray*作用域;
整数计数,i;
NSString*aScope;
计数=范围\计数;
scopes=[[NSMutableArray alloc]initWithCapacity:count];
对于(i=0;i
我在子类
UITableViewController
-viewDidLoad
方法中调用上述方法。不幸的是,当我的表视图控制器get显示在
UITabBarController
中时,什么也没有发生


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

请查看以下中的示例代码: [

回购协议:

它是斯坦福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属性中似乎有一个bug,它没有保留在runloop中

来自contentsController上的文档:“这通常是UITableViewController的一个实例。”。通常,不是必需的。因此,如果您不想使用
UITableViewController
,您只需拥有一个符合协议
UITableViewDataSource
UITableViewDelegate
UITableViewController
。我的问题是
self.searchDisplayController
上的只读属性de>UITableViewController
,但此处的代码正在设置它。该属性在CoreDataTableViewController中重新声明为读/写。不过,提到的repo似乎不再包含此源文件。当您初始化searchDisplayController(并将其设置为强属性以保留它)时它将自动设置您传递给UIViewController的initWithSearchBar…方法的UIViewController的searchDisplayController