简体   繁体   English

iOS Swift:显示的视图控制器为零

[英]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.. 我想知道模态segue显示的模态视图控制器的呈现视图控制器是哪个。

print(self.presentedViewController) is always nil, how is it possible? print(self.presentedViewController)始终为零,这怎么可能?

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: 如果您的代码在刚刚提供了模式VC的VC中,则可以在代码中调用:

print(self.presentedViewController)

to get a reference to the modal VC. 获得对模式 VC的引用。

Weird, it kept returning nil in viewDidLoad , however, when I wrapped my print in timeout, it returned it correctly: 很奇怪,它一直在viewDidLoad返回nil ,但是,当我将print包装在超时中时,它正确地返回了它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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