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") } } |