Warning: Attempt to present View Controller on * which is already presenting <UISearchController: 0x142a1f7c0>

11,942

Solution 1

I was having the same warning and this fixed it for me. You need to stop presenting the search controller so you can present other controller while you are leaving the view.

         override func viewDidDisappear(_ animated: Bool) {

                if SearchController.isActive == true {

                          SearchController.isActive = false

                 }
          } 

Solution 2

I was figuring the same original issue and none of this resolved it for me.

Actually you just have to dismiss the UISearchController as it is said because it is already presenting to the current view.

So, when you want to launch your action, you just have to call this :

if searchController.isActive {
    self.searchController.dismiss(animated: false) { 
        // Do what you want here like perform segue or present
    }
}

Hope this will help!

Solution 3

I'm not sure if the above answers worked properly in previous version but in swift 5, calling dismiss will cause the segue to trigger properly but the search bar will remain active and when they dismiss the triggered segue (come back to the search page) the search bar will look active but the results will not be.

Also dismissing from viewDidDisappear() did not work properly. Here's how I did it.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

//Do some stuff here

   if searchController.isActive{

        searchController.isActive = false

    }
    performSegue(withIdentifier: "<yourSegueIdentifierHere>", sender: nil)

}

Solution 4

Dismissing UISearchController (proposed in this thread in dozen different ways) is a working solution. One more workaround I've found is just having

definesPresentationContext = true 

in viewDidLoad() of a View Controller having UISearchController. This workaround is better in a way the once you navigate back, UISearchController is still showing the search results.

Share:
11,942
Giulio Colleluori
Author by

Giulio Colleluori

Updated on June 06, 2022

Comments

  • Giulio Colleluori
    Giulio Colleluori almost 2 years

    I made a view controller with a UISearchController and a UITableView . There are two different kind of search you can select from the search scope buttons : groups and people. Both searches work and show results on the table. However, if you click on each cell they should direct you to different dynamic pages (a dynamic group page or a dynamic person profile page). The one for groups works, while the one for profiles doesn't. Meaning whenever I click on the a person cell from the results that I got, nothing happens and I get the following warning printed on the console :

    Warning: Attempt to present <MyProject.profileView: 0x13e9df000>  on <MyProject.SearchPage: 0x142a1d8f0> which is already presenting <UISearchController: 0x142a1f7c0>
    

    If you have any idea why this could be happening it'd be really appreciated if you could let me know.

    EDIT : Here's the function that should link the cell to the different view controllers :

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
            if self.searchForGroups {
    
                let detailCell:PFObject = self.filteredGroups.objectAtIndex(indexPath.row) as! PFObject
    
                let vc = self.storyboard!.instantiateViewControllerWithIdentifier("DynamicCellView") as! DynamicCellView
    
                vc.GroupId = detailCell.objectId!
    
                self.presentViewController(vc, animated: false, completion: nil)
    
            }else{
                //Link to use profile
                let user = self.peopleResults[indexPath.row]
                let vc = self.storyboard!.instantiateViewControllerWithIdentifier("ProfileView") as! profileView
                vc.profileId = user.objectId!
                self.presentViewController(vc, animated: true, completion: nil)
            }
        }
    
  • Giulio Colleluori
    Giulio Colleluori about 8 years
    Thanks for the answer. I just updated the question with the code for the function that links the cells to the view controllers. About the 'ProfileView' without uppercase letter you're completely right I didn't write it. For some reason I didn't notice it, I'll change it now. Thanks.
  • Giulio Colleluori
    Giulio Colleluori about 8 years
    Also then I don't get why one of them works and the other doesn't, should both the types not work if that was the case?
  • Baxter Lopez
    Baxter Lopez about 8 years
    Theres a tricky thing going on here. One of your vc (the ones that works) is not being animated when presented while the other does. Maybe the presented view controller is dismissed immediately when not animated and thats why you are not getting the warning.
  • Baxter Lopez
    Baxter Lopez about 8 years
    From Apple documentation: To customize the presentation or dismissal of the search results controller, assign an object to the search controller’s delegate property. Delegate objects must conform to the UISearchControllerDelegate protocol. You use the methods of that protocol to be notified when the search controller itself is activated and when the search results controller is presented or dismissed.
  • Giulio Colleluori
    Giulio Colleluori about 8 years
    Unfortunately I use the UISearchController just for the search bar. I'm not really using it in the proper way as I was having some bigger issues. However, I found out that the link to the groups works only when the search controller has not been selected yet. So when I focus on the search bar and the scope buttons come out it doesn't work at all anymore.
  • Giulio Colleluori
    Giulio Colleluori about 8 years
    Also, I tried to change the animation of the second vc but nothing changed. I think the problem is the search controller itself. Thanks for the help.
  • M Hamayun zeb
    M Hamayun zeb almost 2 years
    Outstanding Working For Me.....