Swift – 从appDelegate的pushViewController,rootViewController.navigationController是零
遇到一些问题,特别是http://blog.originate.com/blog/2014/04/22/deeplinking-in-ios/
我正在设置urlscheme,它运行良好,从另一个应用程序启动应用程序,但通过主机或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; } }
这里是我的版本简化,并迅速从其他几个来源,即获取实例的ViewController从AppDelegate在Swift中我跳过的条件为目前的URL主机,以消除潜在的其他variables的问题。
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 }
迅速版本导致崩溃: fatal error: unexpectedly found nil while unwrapping an Optional value
似乎rootViewController没有导航控制器呢?
在一行代码中:
Swift 3:
self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "view2") as UIViewController, animated: true)
self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("view2") as UIViewController, animated: true)
似乎在我的情况下,rootViewController实际上是UINavigationControllertypes,所以在声明上强制转换允许我直接调用pushToViewController。
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool { var rootViewController = self.window!.rootViewController as UINavigationController let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) var profileViewController = mainStoryboard.instantiateViewControllerWithIdentifier("profile") as ProfileViewController rootViewController.pushToViewController(profileViewController, animated: true) return true }
APPDELEGATE TO PAGE:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let loginPageView = mainStoryboard.instantiateViewControllerWithIdentifier("leadBidderPagerID") as! LeadBidderPage var rootViewController = self.window!.rootViewController as! UINavigationController rootViewController.pushViewController(loginPageView, animated: true)
页面到页面:
let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage self.navigationController?.pushViewController(loginPageView, animated: true)
更新为3/4。 由于在“自我”中没有导航控制器,所以投票最多的“一行代码”不起作用,
let rootViewController = self.window!.rootViewController as! UINavigationController let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController rootViewController.pushViewController(profileViewController, animated: true)
func pushNewView() { if let wind = UIApplication.sharedApplication().delegate?.window { if let rootViewController = wind?.rootViewController { let viewToPush = YourViewController() let nav1 = UINavigationController(rootViewController: viewToPush) if let alreadySomeOneThere = rootViewController.presentedViewController { alreadySomeOneThere.presentViewController(nav1, animated: true, completion: nil) }else { rootViewController.presentViewController(nav1, animated: true, completion: nil) } } } }
- 从AppDelegate呈现UIAlertController
- 表格添加SWRevealViewController后不加载数据
- 如何从AppDelegate中移动这些stream程,并从每个Call中设置个别代理?
- 收到iOS推送通知时打开视图控制器
- 从AppDelegate显示两个ViewController
- 改变本地,phonegap /cordova内置的iOS应用程序的键盘外观
- 在iOS应用程序处于后台甚至死亡时获取位置信息
- 在AppDelegate中使用Swift UINavigationBar setBackgroundImage
- iOS:如何在X小时内每隔一小时在睡眠模式下自动发送电子邮件与iPad?