简体   繁体   中英

Activate UISearchDisplayController from another ViewController

I have an app that would benefit from a search function on the main screen. I have written some code that halfway accomplishes this task, but it won't find results and it crashes when tapping (x) on the UISearchBar. I have been racking my brain trying to figure out why its not working.

Main View Controller (with 'search' bar on main screen):

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


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

ViewController (view with tableview and 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];



}

Turns out, it had nothing to do with proper activation. Basically, I had an array that I was filtering, but it was initialized when the view loaded. Well, since I was calling the view very round-a-bout, it never downloaded the array. Instead I downloaded the array from the Main view controller and searched that in the search view controller. Problem solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM