[英]Dismissing a modal view controller with Search Bar cancel button
I've got a modal view controller with search bar and tableview, basically a pop-over search-box, presented using a pop-over segue. 我有一个带有搜索栏和tableview的模态视图控制器,基本上是一个弹出式搜索框,使用pop-over segue呈现。 At the top it has a UISearchBar with a cancel button.
在顶部有一个带取消按钮的UISearchBar。 I'm trying to dismiss the view controller using the cancel button on that search bar.
我正在尝试使用该搜索栏上的取消按钮关闭视图控制器。
I tried quite a few approaches... 我尝试了很多方法......
-(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self dismissViewControllerAnimated:YES completion:nil];
}
and 和
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
and delegate methods along the lines of 和委托方法
[self.delegate dismissModalViewController:self]
with 同
-(void) dismissModalViewController:(UIViewController*) viewToDismiss
{
[viewToDismiss dismissViewControllerAnimated:YES completion:nil];
}
I don't know whether the UISearchBar is interfering but it seemed a reasonable hypothesis. 我不知道UISearchBar是否在干扰,但这似乎是一个合理的假设。 Otherwise this is a common topic and I apologise for asking a question that may well have been answered before but I've read the fm and googled till I'm blue and still no results.
否则这是一个常见的话题,我为提出一个之前可能已经回答的问题而道歉但是我已经阅读了fm并用谷歌搜索直到我是蓝色但仍然没有结果。
I experienced the same thing in a UIPopoverPresentationController within which I use a UISearchController to filter a tableview. 我在UIPopoverPresentationController中经历了同样的事情,我在其中使用UISearchController来过滤tableview。
The problem is that the first time you call dismissViewController it dismisses the UISearchController, but there are no effects on the UI, so it's easy to think nothing happened. 问题是,第一次调用dismissViewController它会解散UISearchController,但是对UI没有任何影响,因此很容易想到没有发生任何事情。 This is the UISearchBar interfering as you mention.
如你所说,这就是UISearchBar的干扰。
A solution is to call dismissViewController twice (I don't like this), or to call searchController.dismissViewController followed by self.dismissViewController. 一个解决方案是两次调用dismissViewController(我不喜欢这个),或者调用searchController.dismissViewController,然后调用self.dismissViewController。
Swift 3 example... Swift 3示例......
if searchController.isActive {
searchController.dismiss(animated: true, completion: {
self.dismiss(animated: true)
})
} else {
dismiss(animated: true)
}
I ran into the same issue and it's very frustrating that there doesn't seem to be a better way to solve this. 我遇到了同样的问题,并且非常令人沮丧的是,似乎没有更好的方法来解决这个问题。 I did this when dismissing the modal, not ideal but it's pretty smooth:
我在解雇模态时这样做了,不理想,但它很顺利:
if(self.searchController.isActive){
[self.searchController dismissViewControllerAnimated:YES completion:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
});
}];
}else{
[self dismissViewControllerAnimated:YES completion:nil];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.