簡體   English   中英

使用“搜索欄”取消按鈕關閉模態視圖控制器

[英]Dismissing a modal view controller with Search Bar cancel button

我有一個帶有搜索欄和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並用谷歌搜索直到我是藍色但仍然沒有結果。

我在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)
}

我遇到了同樣的問題,並且非常令人沮喪的是,似乎沒有更好的方法來解決這個問題。 我在解雇模態時這樣做了,不理想,但它很順利:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM