简体   繁体   中英

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?

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.

Weird, it kept returning nil in viewDidLoad , however, when I wrapped my print in timeout, it returned it correctly:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.0) {
    print("test", self.presentingViewController)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM