[英]How to present a view controller from appdelegate xcode 11 swift 5
I have been searching all day on how to present a view controller from within the appdelegate.我整天都在搜索如何从 appdelegate 中呈现视图 controller。 It appears that in xcode 11 the window property was moved to the scenedelegate which has been confusing me.
似乎在 xcode 11 中,window 属性被移到了让我感到困惑的场景代表。 I want to present a view controller from within the appdelegate from the didReceiveRemoteNotification function so when the user receives a notification it takes them to a separate view controller with information.
我想从 didReceiveRemoteNotification function 的 appdelegate 中展示一个视图 controller 所以当用户收到通知时,它会将他们带到一个单独的视图 Z594C103F2C6E01E03D8AB059F03 I have tried to do:
我试图这样做:
self.window?.rootViewController?.present(LoginViewController(), animated: false, completion: nil)
within the appdelegate which used to work in a previous application of mine but it does not seem to work anymore.在以前在我以前的应用程序中工作的 appdelegate 中,但它似乎不再工作了。 Any help would be much appreciated.
任何帮助将非常感激。
I was able to solve this issue by using shared windows to get the window from scenedelegate to present the view controller on.我能够通过使用共享 windows 从 scenedelegate 获取 window 以呈现视图 controller 来解决此问题。
UIApplication.shared.windows.first?.rootViewController?.present(vc, animated: false, completion: nil)
Best approach to present view controller through app delegate is without falling for hierarchy like below:通过应用程序委托呈现视图 controller 的最佳方法是不属于如下层次结构:
if let vc = UIStoryboard(name: "YOURSTORYBOARD", bundle: nil).instantiateViewController(withIdentifier: "YOURVIEWCONTROLLER") as? YOURVIEWCONTROLLER {
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentController = currentController.presentedViewController {
currentController = presentController
}
currentController.present(vc, animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.