我正在寻找一种方法,在点击按钮后如何使用搜索栏显示搜索视图。我想用搜索栏创建一个新的UIView,然后在按钮被点击到那个UIView之后?或者有更好的选择吗?
您可以想象在Youtube应用中搜索,但在键入时会看到搜索结果。
修改 所以现在我设法在点击按钮后显示搜索栏但是它没有正确显示。问题是,当搜索栏向下移动时,它会从下移:
我的代码:
implementation ViewController {
NSInteger listNo;
DataClass *dataClass;
UITableViewController *searchResultsController;
}
- (void)viewDidLoad {
[super viewDidLoad];
dataClass = [DataClass getInstance];
self.navigationController.navigationBarHidden = YES;
// Prepare Search Controller and Result Controller
searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y + 5, self.searchController.searchBar.frame.size.width, 44.0);
[self.searchController.searchBar sizeToFit];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = @"Ahoj";
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
- (void)didDismissSearchController:(UISearchController *)searchController {
}
// Load data to next view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showTable"]) {
AdditivesTableViewController *atvc = (AdditivesTableViewController *)segue.destinationViewController;
switch (listNo) {
case 0:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.nonDangerousAdditives];
break;
case 1:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.mediumDangerousAdditives];
break;
case 2:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.dangerousAdditives];
break;
case 3:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.highDangerousAdditives];
break;
default:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.allAdditives];
break;
}
}
}
#pragma mark - IBActions
- (IBAction)nonDangList:(id)sender {
listNo = 0;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)medDangList:(id)sender {
listNo = 1;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)dangList:(id)sender {
listNo = 2;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)highDangList:(id)sender {
listNo = 3;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)searchAdditive:(id)sender {
[self.view addSubview:self.searchController.searchBar];
[self.searchController setActive:YES];
[self.searchController.searchBar becomeFirstResponder];
}
@end