在我的应用程序中,当使用searchController并选择了UITableView时...当返回MasterView时,searchController会藏在NavigationController下。
当未使用searchController并选择了UITableView时... searchController的行为类似于返回masterView时的样子。
请参阅随附的视频。 video of behavior
所有视图都具有来自IB的这些设置。
这就是我设置uisearchcontroller
的方式- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self databaseAddition];
ListString = [NSMutableString string];
store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeReminder
completion:^(BOOL granted, NSError *error) {
// Handle not being granted permission
}];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(topAction:)];
self.navigationItem.rightBarButtonItem = addButton;
UIBarButtonItem *filterButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(filterAction:)];
self.navigationItem.leftBarButtonItem = filterButton;
self.navigationItem.backBarButtonItem.title = @"Devices";
//instantiate a search results controller for presenting the search/filter results (will be presented on top of the parent table view)
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
// Create the search controller with this controller displaying the search results
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.placeholder= @"Search hostname, Tag, Serial, MAC, Model";
self.searchController.hidesNavigationBarDuringPresentation=FALSE;
//self.searchController.searchBar.showsScopeBar=TRUE;
//self.searchController.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"All", @"Win7",@"Win10",@"Mac", nil];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.delegate = self;
self.definesPresentationContext = YES;
self.searchController.searchBar.text = query;
//self.navigationItem.leftBarButtonItem = self.editButtonItem;
//UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItem target:self action:@selector(insertNewObject:)];
//self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
[self updateSearchResultsForSearchController:self.searchController];
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;}
下面是searchResults函数
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
//get search text from user input
//[NSFetchedResultsController deleteCacheWithName:@"FIVE"];
// Init a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Apply an ascending sort for the color items
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"deviceName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:descriptors];
if(AdvSearchON==TRUE)
{
AdvSearchON=FALSE;
fetchRequest.predicate = coPredicate;
NSError *error;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = self;
if (![[self fetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);
// Recover query
query = self.searchController.searchBar.text;
if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"deviceName contains[c] %@ or assetTag contains[c] %@ or addressNIC contains[c] %@ or serialNumber contains[c] %@ or model contains[c] %@", query, query, query, query, query];
// Init the fetched results controller
NSError *error;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = self;
if (![[self fetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);
}
//now that the tableSections and tableSectionsAndItems properties are updated, reload the UISearchController's tableview
[self.tableView reloadData];
NSString *TitleCount = [NSString stringWithFormat:@"%lu Devices", [self.fetchedResultsController.fetchedObjects count]];
self.title = TitleCount;NSLog(@"Table code count hit...");}