【问题标题】:Dismissing a modal view controller with Search Bar cancel button使用搜索栏取消按钮关闭模态视图控制器
【发布时间】:2013-06-13 21:20:47
【问题描述】:

我有一个带有搜索栏和 tableview 的模态视图控制器,基本上是一个弹出式搜索框,使用弹出式 segue 呈现。在顶部它有一个带有取消按钮的 UISearchBar。我正在尝试使用该搜索栏上的取消按钮来关闭视图控制器。

我尝试了很多方法...

-(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

和沿线的委托方法

[self.delegate dismissModalViewController:self]

-(void) dismissModalViewController:(UIViewController*) viewToDismiss
{
    [viewToDismiss dismissViewControllerAnimated:YES completion:nil];
}

我不知道 UISearchBar 是否有干扰,但这似乎是一个合理的假设。否则,这是一个常见的话题,我很抱歉提出一个以前可能已经回答过的问题,但我已经阅读了 fm 并在谷歌上搜索,直到我发青但仍然没有结果。

【问题讨论】:

  • 点击取消后会发生什么?
  • 确保将搜索栏委托设置为呈现的视图控制器。
  • 应用没有任何反应。也就是说,代表已正确设置并且所有函数都已调用。

标签: ios uisearchbar modalviewcontroller uisearchcontroller


【解决方案1】:

我在 UIPopoverPresentationController 中遇到了同样的事情,我在其中使用 UISearchController 过滤表视图。

问题是,第一次调用dismissViewController时,它会关闭UISearchController,但是对UI没有任何影响,所以很容易认为什么都没发生。这是你提到的 UISearchBar 干扰。

解决方案是调用两次dismissViewController(我不喜欢这样),或者先调用searchController.dismissViewController,然后调用self.dismissViewController。

Swift 3 示例...

if searchController.isActive {
    searchController.dismiss(animated: true, completion: { 
        self.dismiss(animated: true) 
    })
} else {
    dismiss(animated: true)
}

【讨论】:

  • 天哪,我不敢相信这是解决方案...谢谢 +1
  • 效果很好,谢谢!如果你为 searchController 设置animated: false,它看起来会更好。
【解决方案2】:

我遇到了同样的问题,但似乎没有更好的方法来解决这个问题,这让我非常沮丧。我在关闭模态时这样做了,虽然不理想但很流畅:

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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2011-03-19
    相关资源
    最近更新 更多