2016-11-10 109 views
2

我想知道是否有方法在應用程序中全局檢測屏幕切換,例如,從應用程序委託?我知道有像isMovingFromParentViewController()和isMovingToParentViewController()方法,但只能在本地使用。任何信息會有幫助,謝謝!使用Swift在iOS中全局檢測屏幕切換(切換)

克里斯蒂安

+0

您可以覆蓋'viewWillAppear'或'viewDidAppear'併發布通知。 – alexburtnik

+0

嗨,如果你的應用中的所有'UIViewController's使用'UINavigationController'作爲'ContainerView',它可以從'AppDelegate'全局檢測到。你可以查看我的答案以獲得更多信息 – KrishnaCA

+0

嗨,你有沒有找到解決這個問題的方法? – KrishnaCA

回答

0

如果你要在AppDelegate以下列方式使用全局UINavigationControllerrootViewControllerUIWindow

let navigationController:UINavigationController = UINavigationController(rootViewController: ViewController()) 
navigationController.delegate = self 

window = UIWindow(frame: UIScreen.main.bounds) 
window?.rootViewController = navigationController 
window?.makeKeyAndVisible() 

然後在UINavigationControllerDelegate以下的委託方法將做的工作在AppDelegate

func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { 

} 

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { 

} 

上述功能將幫助您,當一個新的UIViewControllerUINavigationController顯示出來。不幸的是,有沒有這樣的方法與UIWindow相關來觀察這些變化。請讓我知道它是否有幫助。