Warning: Attempt to present * on * which is already presenting (null)

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

This is my first application for iOS.

So I have a UIVIewController with a UITableView where I have integrated a UISearchBar and a UISearchController in order to filter TableCells to display

override func viewDidLoad() {     menuBar.delegate = self     table.dataSource = self     table.delegate = self     let nib = UINib(nibName: "ItemCellTableViewCell", bundle: nil)     table.registerNib(nib, forCellReuseIdentifier: "Cell")      let searchButton = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "search:")     menuBar.topItem?.leftBarButtonItem = searchButton     self.resultSearchController = ({         let controller = UISearchController(searchResultsController: nil)         controller.searchResultsUpdater = self         controller.dimsBackgroundDuringPresentation = false         return controller     })()     self.table.reloadData() } 

I am using also a modal segue in order to open the element's ViewController where I will display details of the element.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {     self.index = indexPath.row     self.performSegueWithIdentifier("ItemDetailFromHome", sender: self) }  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {     if (segue.identifier == "ItemDetailFromHome") {         let settingsVC = segue.destinationViewController as! ItemDetailViewController         settingsVC.parent = self         if self.isSearching == true  && self.searchText != nil && self.searchText != ""  {             settingsVC.item = self.filteredItems[self.index!]         } else {             settingsVC.item = self.items[self.index!]         }      } } 

That works fine until I try to display the ItemDetailViewController for a filtered element (through the UISearchController).

I have the following message :

Warning: Attempt to present <ItemDetailViewController: *>  on <HomeViewController: *> which is already presenting (null) 

At every time I am going to the ItemDetailViewController.viewDidLoad() function but after that when the search is activated I have the previous error.

Any idea ? I have tried to use the following async dispatch but without success

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {     self.index = indexPath.row     dispatch_async(dispatch_get_main_queue(), { () -> Void in         self.performSegueWithIdentifier("ItemDetailFromHome", sender: self)     }) } 

回答1:

I have found out a solution.

I have add the following code in HomeViewController.viewDidLoad and that works !

definesPresentationContext = true 


回答2:

In my case, I found my code to present the new viewController (a UIAlertController) was being called twice.

Check this before messing about with definesPresentationContext.



回答3:

I faced the same kind of problem What I did is from Interface builder selected my segue Its kind was "Present Modally" and its presentation was "Over current context"

i changed the presentation to "Default", and then it worked for me.



回答4:

This happened with me on our project. I was presenting our log in/log out ViewController as a pop-over. But whenever I tried to log back out again and display the pop-over again, I was getting this logged out in my console:

Warning: Attempt to present UIViewController on <MY_HOME_VIEW_CONTROLLER> which is already presenting (null)

My guess is that the pop-over was still being held by my ViewController even though it was not visible.

However you are attempting to display the new ViewController, the following code I used to solve the issue should work for you:

func showLoginForm() {      // Dismiss the Old     if let presented = self.presentedViewController {         presented.removeFromParentViewController()     }      // Present the New     let storyboard = UIStoryboard(name: "MPTLogin", bundle: Bundle(for: MPTLogin.self))     let loginVC = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as? MPTLogInViewController     let loginNav = MPTLoginNav(rootViewController: loginVC!)     loginNav.modalPresentationStyle = .pageSheet;     self.present(loginNav, animated: true, completion: nil) } 


回答5:

In my case I was trying to present a UIAlertController at some point in the app's lifetime after using a UISearchController in the same UINavigationController.

I wasn't using the UISearchController correctly and forgot to set searchController.isActive = false before dismissing. Later on in the app I tried to present the alert but the search controller, though not visible at the time, was still controlling the presentation context.



回答6:

This is what finally worked for me, as my project didn't exactly have a NavigationVC but instead, individual detached VC's. as xib files

This code produced the bug:

present(alertVC, animated: true, completion: nil) 

This code fixed the bug:

 if presentedViewController == nil{         navigationController?.present(alertVC, animated: true, completion: nil)     } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!