UITableView 重新出现后,UISearchBar 无法成为第一响应者 [英] UISearchBar cannot become first responder after UITableView did re-appear

查看:53
本文介绍了UITableView 重新出现后,UISearchBar 无法成为第一响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于 iOS8 的 iPhone 应用程序.UIPageViewController 有一个 UITableViewController 的子视图,这就是我放置 UISearchController 的地方.

I have an iPhone app for iOS8. UIPageViewController has a child view of UITableViewController, and that's where I put UISearchController.

当第一次出现 UITableViewController 时,我可以通过这样做来激活搜索

When the UITableViewController is presented for the first time, I have no trouble activating the search by doing

[self.searchController.searchBar becomeFirstResponder];

然而,一旦视图控制器通过推送到另一个表视图控制器或以模态呈现 UIViewController(视觉屏蔽它)变得不可见,searchBar 在它重新出现后就不能再成为第一响应者.

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.

搜索本身可以通过

 [self.searchController setActive:YES];

但是,没有插入符号以这种方式闪烁(用户必须在搜索栏内点击才能开始输入).

but, there is no caret blinking this way (user has to tap inside searchBar to start typing).

我有几乎相同的 UITableViewController 和 UISearchController 没有发生事故.所以,我一定遗漏了一些东西,但我想请教一下我应该注意哪个方向来解决这个问题.

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;

推荐答案

我通过 Xcode > 解决了它产品 >分析.

我只是忘记添加

[super viewWillAppear:(BOOL)animated];

inside -(void)ViewWillAppear:(BOOL)animated 在相应的 UITableViewController 上.

inside -(void)ViewWillAppear:(BOOL)animated on the corresponding UITableViewController.

希望我的错误可以在未来节省某人的时间.

Hopefully, my mistake can save someone's time in the future.

这篇关于UITableView 重新出现后,UISearchBar 无法成为第一响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆