如何在Appdelegate中展示一个视图控制器,Xcode 11,Swift 5。

3

我一整天都在搜索如何在appdelegate中展示一个视图控制器。在xcode 11中,window属性被移动到了scenedelegate中,这让我感到困惑。我想在didReceiveRemoteNotification函数中从appdelegate中展示一个视图控制器,所以当用户收到通知时,它会带他们到一个单独的视图控制器中查看信息。我尝试过:

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

在我的以前的应用程序中,这个在appdelegate中工作过,但现在似乎不起作用了。任何帮助将不胜感激。


难点是展示视图控制器还是接收远程通知? - matt
您可以从UIApplication对象获取connectedScenes。您需要选择一个以显示新的视图控制器。 - Paulw11
最困难的部分是呈现视图控制器。我能够接收通知。 - George
2个回答

6
我通过使用共享窗口来获取Scenedelegate中的窗口并在其上呈现视图控制器,成功解决了该问题。
UIApplication.shared.windows.first?.rootViewController?.present(vc, animated: false, completion: nil)

0
最佳方法是通过应用程序委托呈现视图控制器,而不会陷入以下类似的层次结构:
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)
   }
} 

我不使用故事板。 - George
1
@KushalShrestha 很高兴能帮到你。 - Abdul Karim Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接