Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 382
Mostafa Sultan Avatar asked Aug 25 '15 10:08

Mostafa Sultan


3 Answers

This will work if your navigationController is the rootViewController

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

        }
like image 181
Rohit Kumar Avatar answered Oct 24 '22 06:10

Rohit Kumar


Swift 4 answer:

if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
//Do something
}
like image 25
Mutawe Avatar answered Oct 24 '22 06:10

Mutawe


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.

like image 3
dangnabit Avatar answered Oct 24 '22 08:10

dangnabit