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

6

6

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)
        }
    }
Deviate answered 19/4, 2016 at 23:10 Comment(4)
May be you have to present it over UISearchController. self.searchController presentViewController...?Madeup
I just updated the question with the code of the function that links the cells to the view controllers.Deviate
@Madeup I don't think I completely got what you mean. Could you explain? Thanks for the answer.Deviate
Have you tried to use a segue instead of instantiating the view controller? You can call 'performSegueWithIdentifier("segueID")'Formidable
H
10

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

                 }
          } 
Heterocyclic answered 10/1, 2017 at 14:30 Comment(0)
P
10

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!

Petronella answered 22/6, 2017 at 16:1 Comment(0)
P
3

I hit the same error while trying to perform a segue when tapping on a search result. This isn't an ideal workaround but dismissing the searchController before performing the segue fixed it for me:

    self.searchController.dismiss(animated: false) { 
        self.performSegue(withIdentifier: "<YOUR SEGUE IDENTIFIER>", sender: cell)
    }
Plover answered 25/2, 2017 at 17:1 Comment(1)
Outstanding.....Tableau
T
2

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)

}
Tedesco answered 5/4, 2019 at 13:52 Comment(0)
I
1

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.

Integumentary answered 8/12, 2020 at 15:25 Comment(0)
F
0

Without the code is kind of difficult to help you. That error may be happening because you are breaking the view controller hierarchy.

The message is saying that you have 3 view controllers involved:

 SearchPage is presenting UISearchController

 profileView is not yet presented but should be presented on UISearchController or should replace it (For that UISearchController should be dismissed first)

Take in mind that a view controller can present only 1 view controller at the time but it can have several child view controllers (such as a navigation controller).

For more information you can check: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/TheViewControllerHierarchy.html#//apple_ref/doc/uid/TP40007457-CH33-SW1

Just as a comment, its a good coding practice to start your class name with a uppercase letter ('ProfileView' instead of 'profileView')

Formidable answered 20/4, 2016 at 6:4 Comment(6)
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.Deviate
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?Deviate
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.Formidable
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.Formidable
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.Deviate
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.Deviate

© 2022 - 2024 — McMap. All rights reserved.