ios 在 Swift 中从 AppDelegate 获取 ViewController 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24638376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:16:17  来源:igfitidea点击:

Get Instance Of ViewController From AppDelegate In Swift

iosswiftappdelegate

提问by PretzelJesus

I am trying to load a specific ViewController from the app delegate in swift when a user clicks a UILocalNotification. I have figured out that this is called in this function:

当用户单击 UILocalNotification 时,我试图从应用程序委托中快速加载特定的 ViewController。我发现这是在这个函数中调用的:

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!)

But when I try and access one of the open ViewControllers I think it's returning null because my application is crashing. Here is what I am trying:

但是当我尝试访问其中一个打开的 ViewControllers 时,我认为它返回 null,因为我的应用程序崩溃了。这是我正在尝试的:

var rootViewController = self.window!.rootViewController
var storyBoard = rootViewController.storyboard
var setViewController = storyBoard.instantiateViewControllerWithIdentifier("CurrentShows") as ViewController_CurrentShows

rootViewController.navigationController.popToViewController(setViewController, animated: false)
setViewController.reloadData()

It's crashing on the popToViewController line.

它在 popToViewController 行上崩溃了。

回答by fulvio

You could try:

你可以试试:

let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows") as! DetailViewController
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)

Swift 3:

斯威夫特 3:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = mainStoryboard.instantiateViewController(withIdentifier: "viewController")
self.present(viewController, animated: true, completion: nil)