swift - 如何检测推送的 View Controller 是否再次出现?

标签 swift uiviewcontroller uinavigationcontroller

  • 假设我有一个 View Controller (vcA),可以推送 QRCodeScannerViewcontroller (vcB)。当(vcB)扫描某些内容时,它会推送ResultviewController(vcC)

-这 3 个 View 连接到 UInavigation Controller

-用户点击(vcC)上的后退按钮

我的问题是:

1) 如何在不更改 (vcB) 上的代码的情况下知道 (vcB) 是否可见? (vcB) 是一个 Pod

2)我将该代码放在哪里?我只能访问(vcA)

我尝试在(vcA)上添加此代码,但什么也没发生

override func viewDidDisappear(_ animated: Bool) {

    if (vcB.isViewLoaded && (vcB.view.window != nil)){
        print("vcb did appear!")
    }

}

最佳答案

要知道导航堆栈中是否存在 cvB 类的实例,您可以使用这段代码:

let result = self.navigationController?.viewControllers.filter({
    if let vcB = $0 as? UIViewController { // Replace UIViewController with your class, for example ViewControllerB
        return true
    }
    return false
})

if result.isEmpty {
    print("An instance of vcB's class hasn't been pused before")
} else {
    print("An instance of vcB's class has been pused before")
}

关于swift - 如何检测推送的 View Controller 是否再次出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44801639/

相关文章:

swift - AVPlayer : Unable to stream-play . mp4 & .mpg 文件

xcode - 我需要 Xcode 7 和 Swift 2.0 来更新我的应用程序吗

ios - 设置 Sub-UIViewController 框架高度

加载新 Controller 时,iPhone 横向模式切换为纵向模式

iphone - 屏幕上的多个 ViewController

ios - 如何在 Swift 3 中为来自 web 服务的 Json 响应创建模型类

ios - Xcode 和 Swift。宽度是自动计算的,但在 iPhone Xr 上一切正常,在 iPhone X 上看起来很糟糕

ios - 将子 Controller 添加到 TableView 单元格时不会调用 viewWillAppear

iphone - 删除 UITableView 中 UINavigationController 上方的灰色条

iphone - 如何在 Apple 示例 MultipleDetailViews 的详细 View 中推送 View Controller ?