当 UISearchController 处于活动状态时呈现 UIAlertController
Presenting UIAlertController when UISearchController is Active
我有一个 UITableView,它的导航栏中有一个 UISearchController。 table 视图内容随着用户在搜索栏中键入内容而更新。当一行被 selected 时,另一个 table 视图控制器被推送到导航堆栈。在那个新的 table 视图控制器上,有一个显示操作 sheet 的条形按钮项。问题是,当我在使用搜索控制器后 select 一行然后尝试在新的 table 视图控制器上打开操作 sheet 时,我得到
Attempt to present <UIAlertController: 0x103971800> on <KYFB_Black_Book.MembersTableViewController: 0x103854c00> which is already presenting (null)
如果我从第一个控制器 select 一行而不搜索,然后在第二个控制器上打开操作 sheet,一切正常。
在第一个控制器的 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
}
正在根据搜索文本更新结果:
@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()
}
在第二个控制器中,显示警报控制器:
@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)
}
你能试试看是否有效吗?只需替换代码中显示操作的最后一行 sheet.
if let presentedVC = presentedViewController {
presentedVC.present(emailAlert, animated: true, completion: nil)
} else {
present(emailAlert, animated: true, completion: nil)
}
我在使用 UISearchController 并尝试呈现 UIAlertController 时遇到了同样的错误。试试这个:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// To avoid the problem
search.dismiss(animated: false, completion: nil)
}
我有一个 UITableView,它的导航栏中有一个 UISearchController。 table 视图内容随着用户在搜索栏中键入内容而更新。当一行被 selected 时,另一个 table 视图控制器被推送到导航堆栈。在那个新的 table 视图控制器上,有一个显示操作 sheet 的条形按钮项。问题是,当我在使用搜索控制器后 select 一行然后尝试在新的 table 视图控制器上打开操作 sheet 时,我得到
Attempt to present <UIAlertController: 0x103971800> on <KYFB_Black_Book.MembersTableViewController: 0x103854c00> which is already presenting (null)
如果我从第一个控制器 select 一行而不搜索,然后在第二个控制器上打开操作 sheet,一切正常。
在第一个控制器的 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
}
正在根据搜索文本更新结果:
@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()
}
在第二个控制器中,显示警报控制器:
@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)
}
你能试试看是否有效吗?只需替换代码中显示操作的最后一行 sheet.
if let presentedVC = presentedViewController {
presentedVC.present(emailAlert, animated: true, completion: nil)
} else {
present(emailAlert, animated: true, completion: nil)
}
我在使用 UISearchController 并尝试呈现 UIAlertController 时遇到了同样的错误。试试这个:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// To avoid the problem
search.dismiss(animated: false, completion: nil)
}