简体   繁体   English

当UISearchController处于活动状态时显示UIAlertController

[英]Presenting UIAlertController when UISearchController is Active

I have a UITableView that has a UISearchController in the navigation bar. 我有一个UITableView,在导航栏中有一个UISearchController。 The table view contents update as the user types in the search bar. 表格视图的内容随着用户在搜索栏中键入而更新。 When a row is selected another table view controller is pushed to the navigation stack. 当选择一行时,另一个表视图控制器将被推到导航堆栈。 On that new table view controller, there is bar button item that presents an action sheet. 在该新的表格视图控制器上,有显示操作表的条形按钮项。 The problem is that when I select a row after using the search controller and then try to open the action sheet on the new table view controller, I get 问题是,当我在使用搜索控制器后选择一行,然后尝试在新的表格视图控制器上打开操作表时,得到

Attempt to present <UIAlertController: 0x103971800>  on <KYFB_Black_Book.MembersTableViewController: 0x103854c00> which is already presenting (null)

If I select a row from the first controller without searching and then open the action sheet on the second controller, everything works fine. 如果我从第一个控制器中选择一行而不进行搜索,然后在第二个控制器上打开操作表,则一切正常。

In viewDidLoad() of first controller: 在第一个控制器的viewDidLoad()中:

searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.searchBar.tintColor = .white
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = false
        let searchField = searchController.searchBar.value(forKey: "searchField") as? UITextField
        searchField?.textColor = .white

        if #available(iOS 11.0, *) {
            navigationItem.searchController = searchController
            navigationItem.hidesSearchBarWhenScrolling = false
        } else {
            searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 200.0, height: 44.0)
            searchField?.textColor = .black
            searchController.searchBar.tintColor = .black
            navigationItem.titleView = searchController.searchBar
        }

Updating results based on search text: 根据搜索文本更新结果:

@available(iOS 8.0, *)
    func updateSearchResults(for searchController: UISearchController) {
        filteredCountyNames.removeAll(keepingCapacity: false)
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
        let array = (countyNames as NSArray).filtered(using: searchPredicate)
        filteredCountyNames = array as! [String]
        tableView.reloadData()
    }

In second controller, presenting the alert controller: 在第二个控制器中,显示警报控制器:

@objc func actionTapped() {
        let emailAlert = UIAlertController(title: "Contact All Committee Members", message: "", preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) in
            print("Cancel")
            })
        let emailAction = UIAlertAction(title: "Email", style: .default, handler: { (action) in
            self.emailAll()
            })
        let messageAction = UIAlertAction(title: "Text Message", style: .default, handler: { (action) in
            self.textAll()
            })

        emailAlert.view.tintColor = UIColor(red: 28.0 / 255.0, green: 75.0 / 255.0, blue: 136.0 / 255.0, alpha: 1)
        emailAlert.addAction(emailAction)
        emailAlert.addAction(messageAction)
        emailAlert.addAction(cancelAction)
        present(emailAlert, animated: true, completion: nil)
    }

Can you try this to see if it works? 您可以试试看是否有效吗? Just replace the last line of your code where you present the action sheet. 只需替换代码的最后一行即可显示操作表。

if let presentedVC = presentedViewController {
    presentedVC.present(emailAlert, animated: true, completion: nil)
} else {
    present(emailAlert, animated: true, completion: nil)
}

I had the same error, when using a UISearchController and try to present a UIAlertController. 使用UISearchController并尝试显示UIAlertController时出现相同的错误。 Try this: 尝试这个:

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // To avoid the problem
        search.dismiss(animated: false, completion: nil)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM