uinavigationcontroller – Swift – pushViewController从appDelegate,rootViewController.navigationController为零

前端之家收集整理的这篇文章主要介绍了uinavigationcontroller – Swift – pushViewController从appDelegate,rootViewController.navigationController为零前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
具体问题遵循几个指南
http://blog.originate.com/blog/2014/04/22/deeplinking-in-ios/

我正在设置url方案,并且从另一个应用程序启动该应用程序,但是传递主机或url不能像现在这样工作。我正在为所有的视图布局使用故事板和界面构建器。

该指南在appDelegate中显示此openURL:

-(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;
    }
}

这是我的版本简化,并从其他几个来源,即
Get Instance Of ViewController From AppDelegate In Swift我正在跳过url主机的条件,以删除问题中的潜在的其他变量。

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

}

迅速版本导致崩溃:
致命错误:在解开可选值时意外发现为零

看来rootViewController还没有一个navigationController?

一行代码
self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("view2") as UIViewController,animated: true)

猜你在找的Swift相关文章