在 Swift 中从 AppDelegate 获取 ViewController 的实例 [英] Get Instance Of ViewController From AppDelegate In Swift

查看:78
本文介绍了在 Swift 中从 AppDelegate 获取 ViewController 的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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:

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()

它在 popToViewController 行上崩溃了.

It's crashing on the popToViewController line.

推荐答案

你可以试试:

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:

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

这篇关于在 Swift 中从 AppDelegate 获取 ViewController 的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆