最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中判断当前视图控制器是否可见和当前正在显示的是哪个视图

Swift crifan 4685浏览 0评论

swift viewcontroller visible

swift viewcontroller current

ios – How do you check current view controller class in Swift? – Stack Overflow

swift – Get top most UIViewController – Stack Overflow

ios – Get current view controller from the app delegate (modal is possible) – Stack Overflow

Find the current top view controller for your iOS application · GitHub

后来用如下代码:

即可,判断某个UI的ViewController,是否是当前显示的界面:

extension UIApplication {
    class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(presented)
        }
        return base
    }
}
            if let topVC = UIApplication.topViewController() {
                print("topVC=\(topVC), relatedConversationItem.msgTVC=\(relatedConversationItem.msgTVC)")
               
                if topVC != relatedConversationItem.msgTVC {
                    //topVC=<JianDao.ConversationViewController: 0x7ffcfc874230>
                    //relatedConversationItem.msgTVC=<JianDao.MessageTableViewController: 0x7ffcfca4e7d0>
                    //not in current message vc, so need update badge
                    conversationItemList[relatedConversationItemIdx].badgeValue += 1
                }else{
                    //topVC=<JianDao.MessageTableViewController: 0x7fc8397de3c0>
                    //relatedConversationItem.msgTVC=<JianDao.MessageTableViewController: 0x7fc8397de3c0>
                    print("in current message UI, not need update badge")
                }
            }

转载请注明:在路上 » [已解决]swift中判断当前视图控制器是否可见和当前正在显示的是哪个视图

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.196 seconds, using 22.06MB memory