简体   繁体   中英

UISearchBar in UIPopoverController not getting first responder

When a user presses the search bar button, a popover appears with a search bar. I am loading the search results view from a nib that contains just a tableview. Now, I want the search bar to become selected when the popover appears however the search bar is not getting first responder. I have tried setting it in the didPresentSearchController: delegate method but that doesn't work either. Below is the code that I have tried.

- (IBAction)search:(id)sender {
    self.searchView = [[[NSBundle mainBundle] loadNibNamed:@"Popover" owner:self options:nil] objectAtIndex:0];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchView];
    self.searchController.searchBar.scopeButtonTitles = @[@"BLARGH", @"WARGH"];
    self.searchController.searchBar.selectedScopeButtonIndex = 1;
    self.searchController.searchBar.delegate = self;
    self.searchController.delegate = self;

    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.tableHeaderView = self.searchController.searchBar;

    self.popover = [[UIPopoverController alloc] initWithContentViewController:self.searchView];

    [self.popover setPopoverContentSize:CGSizeMake(320, 88)];
    self.popover.delegate = self;

    [self.popover presentPopoverFromBarButtonItem:self.searchButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    [self.searchController.searchBar becomeFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.popover setPopoverContentSize:CGSizeMake(320, 600)];
    self.table.frame = CGRectMake(0, 0, 320, 600);
    NSLog(@"Me pressed");
}

I think it helping you Use this Delegate and also check in .xib or storyboard you have connected delegate of search bar or not

      - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
     {
         [searchBar resignFirstResponder];
     }

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