使用“搜索栏”取消按钮关闭模态视图控制器

时间:2013-06-13 21:20:47

标签: ios uisearchbar modalviewcontroller uisearchcontroller

我有一个带有搜索栏和tableview的模态视图控制器,基本上是一个弹出式搜索框,使用pop-over 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并用谷歌搜索,直到我变成蓝色并且仍然没有结果。

2 个答案:

答案 0 :(得分:7)

我在UIPopoverPresentationController中经历了同样的事情,我在其中使用UISearchController来过滤tableview。

问题是第一次调用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 :(得分:-1)

我遇到了同样的问题,并且非常令人沮丧的是,似乎没有更好的方法来解决这个问题。我在解雇模态时这样做了,不理想,但它很顺利:

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];
}
相关问题