简体   繁体   English

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

[英]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.

相关问题 从appdelegate呈现视图控制器 - Present a view controller from appdelegate 目标 C:如何从 appdelegate 呈现模态视图 controller? - Objective C: How to present modal view controller from appdelegate? Swift:如何关闭从AppDelegate启动的视图控制器和导航控制器 - Swift: how to close view controller and navigation controller launched from AppDelegate 无法从AppDelegate提供视图控制器 - Unable to present a view controller from AppDelegate 来自AppDelegate的Xcode更新视图控制器UIImage - Xcode Update View Controller UIImage from AppDelegate 如何在 Swift 中从 Appdelegate 调用视图控制器中的方法? - how to call a method in a view controller from Appdelegate in Swift? 如何从 AppDelegate.swift 路由到任何视图 controller - How to do routing from AppDelegate.swift to any View controller Swift –从AppDelegate向视图控制器添加子视图 - Swift – Add subview to View Controller from AppDelegate 如何从appdelegate呈现和消除模式视图? - How to present and dismiss modal view from appdelegate? 如何从AppDelegate.swift访问不是根视图控制器的视图控制器? - How to access a view controller that is not the root view controller from AppDelegate.swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM