首页 > 解决方案 > 如何在 Swift 3 中检查当前 ViewController?

问题描述

我打开了ChatViewController,我导航到了其他页面,然后我再次访问了,所以当我访问同一页面时,我ChatViewController如何检查页面上的即时消息。ChatViewController

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(true)

    UserDefaults.standard.value(forKey: "ChatView")

    print("view will appear called")
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)

    UserDefaults.standard.object(forKey: "ChatView")
    print("view will disappear called")
}

我需要设置任何UserDefaults值或其他东西吗?有人可以帮助我吗?在此先感谢

标签: iosswift3nsuserdefaults

解决方案


斯威夫特 5 和 iOS 15

     if self.navigationController?.presentedViewController != nil && 
    self.navigationController?.presentedViewController is ChatViewController {
        return
}

推荐阅读