2015-01-26 31 views
0

我有一个应用程序,将受益于主屏幕上的搜索功能。我已经编写了一些完成此任务的代码,但它不会找到结果,并且在UISearchBar上点击(x)时会崩溃。我一直在试图弄清楚为什么它不起作用。从另一个ViewController激活UISearchDisplayController

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

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


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

视图控制器(查看与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]; 



} 

回答

0

事实证明,它有没有做适当的激活。基本上,我有一个我正在过滤的数组,但它在视图加载时被初始化。那么,因为我非常全面地调用视图,它从来没有下载过这个数组。相反,我从主视图控制器下载数组,并在搜索视图控制器中搜索该数组。问题解决了。

相关问题