簡體   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