繁体   English   中英

如何从 appdelegate xcode 11 swift 5 呈现视图 controller

[英]How to present a view controller from appdelegate xcode 11 swift 5

我整天都在搜索如何从 appdelegate 中呈现视图 controller。 似乎在 xcode 11 中,window 属性被移到了让我感到困惑的场景代表。 我想从 didReceiveRemoteNotification function 的 appdelegate 中展示一个视图 controller 所以当用户收到通知时,它会将他们带到一个单独的视图 Z594C103F2C6E01E03D8AB059F03 我试图这样做:

self.window?.rootViewController?.present(LoginViewController(), animated: false, completion: nil)

在以前在我以前的应用程序中工作的 appdelegate 中,但它似乎不再工作了。 任何帮助将非常感激。

我能够通过使用共享 windows 从 scenedelegate 获取 window 以呈现视图 controller 来解决此问题。

UIApplication.shared.windows.first?.rootViewController?.present(vc, animated: false, completion: nil)

通过应用程序委托呈现视图 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM