使用 swift 从应用程序委托打开视图控制器 [英] Opening view controller from app delegate using swift

查看:19
本文介绍了使用 swift 从应用程序委托打开视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a push notification which determines which view to open according to information obtained from the push.

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

I have managed to get the information from the push, but I am now struggling to get the view to open

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

Looking at other stack overflow questions I have the following currently:

App Delegate 加载完成:

App Delegate Did finish loading:

     //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... 行上失败.

Currently this is failing on the var ViewController = self... line.

推荐答案

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

You have to set ViewController StoryBoardId property as below image.

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

open viewController using coding as below in 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
    }

对于 iOS 13+(基于 dev2qa 的一篇文章)
打开 SceneDelegate.swift 并添加以下内容

For iOS 13+ (based on an article by dev2qa)
Open SceneDelegate.swift and add following

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // If this scene's self.window is nil then set a new UIWindow object to it.
    self.window = self.window ?? UIWindow()

    // Set this scene's window's background color.
    self.window!.backgroundColor = UIColor.red

    // Create a ViewController object and set it as the scene's window's root view controller.
    self.window!.rootViewController = ViewController()

    // Make this scene's window be visible.
    self.window!.makeKeyAndVisible()

    guard scene is UIWindowScene else { return }
}

有一个开源的导航实用程序 试图使这更容易.示例

There is an open-source navigation utility which attempts to make this easier. Example

这篇关于使用 swift 从应用程序委托打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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