swift - 如何在 Swift 中获取父 ViewController 的类名?

标签 swift uiviewcontroller

有没有办法在当前(子)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/

相关文章:

ios - 导航 Controller 和容器 View 之间的通信

从 Swift 调用 C 函数只知道内存地址

ios - 用于固定 UILabel 高度的字符串

iphone - 如何在页面 View Controller 中获取当前 View Controller

ios - 从编程 uibutton 属性访问全局变量

ios - 我如何从 SKScene 移动到 UIViewController

ios - 关闭 UIImagePickerController 时 UITabBar 消失

iphone - 在父 View Controller 和 subview Controller 之间进行通信的更好方法是什么?

ios - 重构代码后使用 self.view

ios - 从未在 UITableViewController 中调用过 cellForRowAtIndexPath