使用swift从app delegate打开视图控制器

Lis*_*ter 57 uiviewcontroller ios appdelegate swift

我正在尝试创建推送通知,根据从推送获得的信息确定要打开的视图.

我已经设法从推送中获取信息,但我现在正在努力让视图打开

查看其他堆栈溢出问题,我目前有以下内容:

App Delegate完成加载:

     //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

                        }
    }
Run Code Online (Sandbox Code Playgroud)

目前这在var ViewController = self ...行上失败了.

Kir*_*odi 99

您必须将ViewController StoryBoardId属性设置为如下图像.

在此输入图像描述

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

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
    }
Run Code Online (Sandbox Code Playgroud)

  • 但这是创建控制器的新实例。我想要已经有状态的控制器。或者我错过了什么? (2认同)

小智 41

斯威夫特3:

当从当前viewController通过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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的方法,你可以解雇这个视图以及self.dismiss(动画:true,完成:nil) (2认同)

小智 11

斯威夫特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)
   }
Run Code Online (Sandbox Code Playgroud)


iDh*_*val 7

首先初始化 window

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

用于设置rootViewController内部AppDelegate

let viewController = storyBoard.instantiateViewControllerWithIdentifier("Circles") as UIViewController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)


未来陆*_*投资人 6

有一个迅捷的4版本

func application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: 
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Circles") as UIViewController
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = initialViewControlleripad
            self.window?.makeKeyAndVisible()

return true}
Run Code Online (Sandbox Code Playgroud)


Amu*_*608 5

在斯威夫特 3

        let mainStoryboard : UIStoryboard = UIStoryboard(name: StorybordName, bundle: nil)
        let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: identifierName) as UIViewController
        if let navigationController = self.window?.rootViewController as? UINavigationController
        {
            navigationController.pushViewController(initialViewControlleripad, animated: animation)
        }
        else
        {
            print("Navigation Controller not Found")
        }
Run Code Online (Sandbox Code Playgroud)