Search code examples
ioswifimodalviewcontroller

iOS Swift: presented view controller is nil


I'm trying to know which is the presented view controller of a modal view controller shown by a modal segue..

print(self.presentedViewController) is always nil, how is it possible?


Solution

  • To get the "parent" view controller of the view controller you are in, you want to get the "presenting" controller:

    print(self.presentingViewController)
    

    If your code is in the VC that just presented a modal VC, in that code you can call:

    print(self.presentedViewController)
    

    to get a reference to the modal VC.