ios - 如何在 Swift 中检查实例的类型?

标签 ios swift unwind-segue

我想使用展开转场从当前 UIViewController 导航到第二个呈现的 UIViewController:

let unwindDestinationViewController = self.presentingViewController!.presentingViewController!
                    switch unwindDestinationViewController{
                    case is UIViewControllerSubclass:
                        self.performSegue(withIdentifier: "unwindToUIViewControllerSubclass", sender: self)
                        break
                    default:
                        break
                    }

但是这不起作用。我还尝试将 case 语句更改为 UIViewControllerSubclass.Type 但它给了我一个错误:从 UIViewController 转换为不相关类型 UIViewControllerSubclass 总是失败。

如果有人能告诉我我做错了什么,我真的很感激。

最佳答案

您的代码对于检查类型是正确的:

case is UIViewControllerSubclass:

测试类的另一种方法:

case let vc as UIViewControllerSubclass:

如果您需要访问特定于该类的方法或属性,这会很有帮助。

如果您不需要使用 vc 则只需执行以下操作:

case _ as UIViewControllerSubclass:
<小时/>

您可以在此处使用 if 语句:

if unwindDestinationViewController is UIViewControllerSubclass {
    self.performSegue(withIdentifier: "unwindToUIViewControllerSubclass", sender: self)
}

关于ios - 如何在 Swift 中检查实例的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44201819/

相关文章:

ios - 展开可选值线程 :1 bad instruction 时意外发现 nil

swift - UISearchBar 与 UINavigationBar 重叠?

ios - UISplitViewController 主视图在详细 View 上调用 PerformSegueWithIdentifier

ios - 如何将不同的 UITapGestureRecognizer 添加到单个 UILabel 的多个部分?

ios - 使用 alamofireimages 缓存

ios - 多维数组输出到标签

ios - 从第二个 View Controller 到第一个 View Controller ios8

html - 背景 : fixed no repeat not working on mobile

ios - AudioKit:从 Podfile 的开发分支中拉取

swift - 没有 UI 元素的 Segue 和 Unwind segue