从另一个ViewController激活UISearchDisplayController

时间:2015-01-26 22:54:51

标签: ios cocoa-touch uisearchbar uisearchdisplaycontroller

我的应用程序可以从主屏幕上的搜索功能中受益。我已经编写了一些中途完成此任务的代码,但是当它在UISearchBar上点击(x)时它找不到结果并且崩溃了。我一直绞尽脑汁想弄清楚它为什么不起作用。

主视图控制器(主屏幕上有'搜索'栏):

 ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];


        [self.navigationController pushViewController:viewController animated:YES];
         [viewController sendSearchRequest:[nameLabel text] sport:[sportLabel text]];

ViewController(使用tableview和UISearchDisplayController查看)

-(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport{
    [self.filteredArray removeAllObjects]; // First clear the filtered array.
    self.searchText = request;

    /*
     * Search the main list for products whose type matches the scope (if
     * selected) and whose name matches searchText; add items that match to the
     * filtered array.
     */
    [self.filteredArray removeAllObjects]; // First clear the filtered array.
    self.searchText = request;

    /*
     * Search the main list for products whose type matches the scope (if
     * selected) and whose name matches searchText; add items that match to the
     * filtered array.
     */
    NSLog(@"Request: %@", request);
    for (Athlete *athlete in self.athletes) {
        NSComparisonResult result = [[athlete name] compare:request options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [request length])];
        if (result == NSOrderedSame) {
            [self.filteredArray addObject:athlete];
        }
    }
    [self.searchDisplayController setActive: YES animated: YES];
    [self.searchDisplayController.searchBar setText:request];



}

1 个答案:

答案 0 :(得分:0)

原来,它与正确的激活无关。基本上,我有一个我正在过滤的数组,但它在加载视图时被初始化。好吧,既然我把这个视图称为非常圆形,它从未下载过数组。相反,我从主视图控制器下载了数组,并在搜索视图控制器中搜索了该数组。问题解决了。