【问题标题】:Get Instance Of ViewController From AppDelegate In Swift在 Swift 中从 AppDelegate 获取 ViewController 的实例
【发布时间】:2014-08-29 13:48:45
【问题描述】:

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

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

但是当我尝试访问打开的 ViewController 之一时,我认为它返回 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()

它在 popToViewController 行上崩溃了。

【问题讨论】:

    标签: ios swift appdelegate


    【解决方案1】:

    你可以试试:

    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)
    

    斯威夫特 3:

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

    【讨论】:

    • 我尝试在函数中使用它,但它不起作用,我使用的是 xcode7 和 swift2。你介意更新上面的代码吗?
    • @suisied 可能值得在 StackOverflow 上创建一个问题并链接到它,因为如果不查看您的代码,我们无法真正诊断您的问题。
    • 不能在 swift 2 中工作.. 无法获得“主要”故事板。
    猜你喜欢
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多