ios - 在推送 View 上使用 UISearchController

标签 ios uisearchcontroller

我已在我的应用中成功实现了 UISearchController。我在要使用它的 View 的 viewDidLoad 方法中像这样设置它:

_profileSearchView = [self.storyboard instantiateViewControllerWithIdentifier:@"profileListView"];
[_profileSearchView initSearchController];
self.navigationItem.titleView = _profileSearchView.searchController.searchBar;

initSearchController 方法初始化搜索 Controller ,该 Controller 是 _profileSearchView 的一个属性,驻留在 _profileSearchView 中:

- (void) initSearchController {
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self];
    _searchController.delegate = self;
    _searchController.hidesNavigationBarDuringPresentation = NO;

    _searchController.searchBar.delegate = self;
    _searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    _searchController.searchBar.showsCancelButton = YES;
    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
}

如果我在导航 Controller 的 Root View 中使用它,效果很好。但是,如果我推送一个 View 并尝试在那里使用它,搜索 Controller 不会变为事件状态,并且此错误会显示在控制台中:

Warning: Attempt to present <UISearchController: 0x7fb113605220> on <RootViewController: 0x7fb11318a6d0> whose view is not in the window hierarchy!

它提示的 RootViewController 是我从中推送的 Root View 。为什么这只能在 Root View 中起作用?

更新: Root View Controller 有 self.definesPresentationContext = YES; (推送的 View 也有),当我从 Root View 中删除它时,搜索 Controller 在推送的 View 上工作。不幸的是,这也破坏了其他一些东西,所以我需要保留它。那么我怎样才能允许 Root View 和推送 View 都有一个单独的功能搜索 Controller 呢?

最佳答案

该问题是由 Root View 和推送 View 均具有 self.definesPresentationContext = YES; 引起的。解决方案是添加以下内容:

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.definesPresentationContext = NO;
}

并确保当 View 出现时它是YES:

-(void) viewWillAppear:(BOOL)animated {
    self.definesPresentationContext = YES;
}

在 Root View 中。 Root View 仍然能够从其自己的搜索 Controller 正确推送搜索结果,推送的 View 也是如此。

关于ios - 在推送 View 上使用 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37489189/

相关文章:

ios - PhoneGap SVG 在 iOS 上作为 data-uri

ios - Twilio IP 消息传递 : What is the point of [client handleNotification] in the iOS messaging client API?

ios - iOS Documents Directory 每次都需要构建吗?

python - Swift,如何从 Pandas JSON 文件导入 JSON 文件

ios - 如何从嵌入在父 View Controller 的 subview Controller 中的 UISearchViewController 中呈现 View Controller ?

ios - 检测同时按下两个或多个 UIButton

ios - 如何更改 UISearchBar 取消按钮的文本颜色?

ios - 切换选项卡后 UIViewcontroller 消失 UITabBarController

ios - UISearchViewController 不允许在解除分配时尝试加载 View Controller 的 View

ios - 如何在 UITableView 中显示/隐藏 UISearchController searchBar?