2015-01-26 57 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

事實證明,它有沒有做適當的激活。基本上,我有一個我正在過濾的數組,但它在視圖加載時被初始化。那麼,因爲我非常全面地調用視圖,它從來沒有下載過這個數組。相反,我從主視圖控制器下載數組,並在搜索視圖控制器中搜索該數組。問題解決了。

相關問題