问题
I have an iPhone app for iOS8. UIPageViewController has a child view of UITableViewController, and that's where I put UISearchController.
When the UITableViewController is presented for the first time, I have no trouble activating the search by doing
[self.searchController.searchBar becomeFirstResponder];
However, once the view controller gets invisible either by pushing to another table view controller, or modally presenting a UIViewController (visually masking it), the searchBar can no longer become first responder after it reappears.
The search itself can be done by
[self.searchController setActive:YES];
but, there is no caret blinking this way (user has to tap inside searchBar to start typing).
I have almost identical UITableViewController with UISearchController without the incident. So, I must be missing something, but I would like your opinion on which direction I should pay attention on solving this.
Below is how I set the search controller.
@interface MonthTableViewController () <UISearchResultsUpdating, UISearchControllerDelegate>
@property (nonatomic, strong) UISearchController *searchController;
UITableViewController *searchController = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
searchController.tableView.dataSource = self;
searchController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
// Hide search bar
CGRect myBounds = self.tableView.bounds;
myBounds.origin.y += self.searchController.searchBar.frame.size.height;
_lastlyAddedTableBoundY = self.searchController.searchBar.frame.size.height;
self.tableView.bounds = myBounds;
回答1:
I solved it by going Xcode > Product > Analyze
.
I simply forgot to add
[super viewWillAppear:(BOOL)animated];
inside -(void)ViewWillAppear:(BOOL)animated
on the corresponding UITableViewController
.
Hopefully, my mistake can save someone's time in the future.
来源:https://stackoverflow.com/questions/27451088/uisearchbar-cannot-become-first-responder-after-uitableview-did-re-appear