Swift - 来自 appDelegate 的 pushViewController,rootViewController.navigationController 为零 [英] Swift - pushViewController from appDelegate, rootViewController.navigationController is nil

查看:27
本文介绍了Swift - 来自 appDelegate 的 pushViewController,rootViewController.navigationController 为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在遵循一些指南时遇到问题,特别是http://blog.originate.com/blog/2014/04/22/deeplinking-in-ios/

Having a problem following a few guides, specifically http://blog.originate.com/blog/2014/04/22/deeplinking-in-ios/

我正在设置 url 方案,从另一个应用程序启动应用程序运行良好,但传入主机或 url 并没有像它应该的那样工作.我正在为所有视图布局使用故事板和界面构建器.

I'm setting the url scheme and it's working well to launch the app from another app, but passing in the host or url are not working as it seems it should. I'm using storyboards and interface builder for all the view layouts.

指南在 appDelegate 中显示了这个 openURL:

The guide shows this openURL in appDelegate:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    if([[url host] isEqualToString:@"page"]){
        if([[url path] isEqualToString:@"/page1"]){
            [self.mainController pushViewController:[[Page1ViewController alloc] init] animated:YES];
        }
    return YES;
    }
}

这是我从其他一些来源简化和快速的版本,即从 Swift 中的 AppDelegate 获取 ViewController 的实例 我正在跳过url 主机目前删除问题中潜在的其他变量的条件.

Here is my version simplified and in swift from a few other sources, ie Get Instance Of ViewController From AppDelegate In Swift I'm skipping the conditional for the url host at the moment to remove potential other variables in the problem.

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
    var rootViewController = self.window!.rootViewController
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var profileViewController = mainStoryboard.instantiateViewControllerWithIdentifier("profile") as ProfileViewController

    rootViewController.navigationController.popToViewController(profileViewController, animated: true)

    return true

}

swift 版本导致崩溃:致命错误:解包可选值时意外发现 nil

The swift version causes a crash: fatal error: unexpectedly found nil while unwrapping an Optional value

rootViewController 好像还没有 navigationController?

It seems that rootViewController doesn't have a navigationController yet?

推荐答案

在我的例子中,rootViewController 实际上是 UINavigationController 类型,所以将它投射到声明上允许我直接在其上调用 pushToViewController.

It seems that rootViewController actually is of type UINavigationController in my case, so casting it on declaration allowed me to call pushToViewController directly on it.

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
    let rootViewController = self.window!.rootViewController as! UINavigationController
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "InstructionVC") as! InstructionVC
    rootViewController.pushViewController(profileViewController, animated: true)
    return true

}

这篇关于Swift - 来自 appDelegate 的 pushViewController,rootViewController.navigationController 为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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