从应用程序委托使用swift打开视图控制器

我正在尝试创build一个推送通知,根据从推送中获得的信息确定要打开哪个视图。

我已经设法从推动中获得信息,但我现在正在努力争取开放的观点

看看其他堆栈溢出问题我目前有以下几点:

应用程序委托完成加载:

//Extract the notification data if let notificationPayload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary { // Get which page to open let viewload = notificationPayload["view"] as? NSString let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) //Load correct view if viewload == "circles" { var viewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier("Circles") as! UIViewController self.window?.rootViewController = viewController } } 

目前这是失败的var ViewController = self …行。

你必须设置ViewController的StoryBoardId属性如下图。

在这里输入图像描述

打开viewController使用编码如下在swift中

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Circles") as UIViewController self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = initialViewControlleripad self.window?.makeKeyAndVisible() return true } 

Swift 3:

当通过AppDelegate从当前视图控制器呈现一个新的viewController时,这是我的首选方法。 这样,在处理推送通知或通用链接时,您不必完全拆除视图层次结构

 if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "someController") as? SomeController { if let window = self.window, let rootViewController = window.rootViewController { var currentController = rootViewController while let presentedController = currentController.presentedViewController { currentController = presentedController } currentController.present(controller, animated: true, completion: nil) } } 

我会说每次你想改变rootViewController创buildUIWindow是个坏主意。 在更改rootVC(使用上层解决scheme)之后,您将在应用中同时拥有多个UIWindows。

在我看来更好的解决办法是:

  1. 获取新的rootVC: let rootVC = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("newRootVCIdentifier") as UIViewController
  2. 从UIScreen的边界为新的rootVC设置框架: rootVC.view.frame = UIScreen.mainScreen().bounds
  3. 为当前窗口设置新的根控制器(这里是animation): UIView.transitionWithView(self.window!, duration: 0.5, options: .TransitionCrossDissolve, animations: { self.window!.rootViewController = rootVC }, completion: nil)

完成!

您不需要方法window?.makeKeyAndVisible() ,导致此解决scheme在当前应用程序窗口上工作。

首先初始化window

 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let storyBoard = UIStoryboard(name: "Main", bundle: nil) 

AppDelegate类中设置rootViewController

 let viewController = storyBoard.instantiateViewControllerWithIdentifier("Circles") as UIViewController self.window?.rootViewController = viewController self.window?.makeKeyAndVisible() 

Swift 3

与导航控制器一起显示视图:

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier :"InboxViewController") as! InboxViewController let navController = UINavigationController.init(rootViewController: viewController) if let window = self.window, let rootViewController = window.rootViewController { var currentController = rootViewController while let presentedController = currentController.presentedViewController { currentController = presentedController } currentController.present(navController, animated: true, completion: nil) }