access navigationController from a viewController that i opened using modal in swift

10,223

Solution 1

Sounds like a job for delegation or notifications, depending on how you want to go about it. In either case—your navigationController can either be notified to perform the segue at the right time, or accessed and instructed to do so as a delegate.

Solution 2

This will work if your navigationController is the rootViewController

if let navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as? UINavigationController {

        }

Solution 3

Swift 4 answer:

if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
//Do something
}
Share:
10,223
Mostafa Sultan
Author by

Mostafa Sultan

IOS Developer

Updated on June 04, 2022

Comments

  • Mostafa Sultan
    Mostafa Sultan almost 2 years

    i am using navigationController (push and pop) but in a special case i have to show the next viewcontroller using modal segue , and when i dismiss that modal i want to pop the last viewController in the navigationController and then push a new one to appear after dismissing the modal one , how can i access the navigationController that is full of viewControllers from a modal viewController i can send in the last element in navigationController as self in prepareForSegue for the modal but i am asking if there is another way , sorry my questions are always complicated