有没有办法在当前(子)UIViewController
中获取父 VC 的类名?我的“子”VC(推送)有两个“父”UIViewController
,所以我想知道哪个是当前父级?
最佳答案
这是一种方法:
if let parentVC = parent as? SomeViewController {
// parentVC is someViewController
} else if let parentVC = parent as? AnotherViewController {
// parentVC is anotherViewController
}
这将有条件地将 parent
(父 View Controller )分配和解包为其适当的类型。在条件 block 中,parentVC
将是正确的类。
也就是说,这是一种代码味道—— subview Controller 通常应该不知道它们的父 View Controller 是谁。无论您要解决什么问题,您都应该用告诉,不要问和委派来解决。
关于swift - 如何在 Swift 中获取父 ViewController 的类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28198638/