AppDelegate Segue替代通行数据 [英] AppDelegate segue alternative pass data

查看:87
本文介绍了AppDelegate Segue替代通行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将数据从我的appDelegate传递给我的一个视图控制器.我尝试使用segues来执行此操作,但这是行不通的.所以我想知道的是;除了使用segues之外,还有其他方法可以呈现另一个视图控制器并将数据传递给它吗?

I am struggling with passing data from my appDelegate to one of my view controllers. I have tried to use segues to do this, but this just doesn't work. So what I am wondering about is; is there any other way to present another view controller and pass data to it, other than using segues?

谢谢.

推荐答案

由于AppDelegate不是ViewController,因此不能使用从AppDelegateViewController的segue. Segues仅适用于ViewControllers.

You can't use segue to ViewController from AppDelegate, because AppDelegate is not a ViewController. Segues are only work with ViewControllers.

但是您可以从AppDelegateViewController初始化故事板标识符,以便发送数据.

But you can initiate a storyboard identifier from your AppDelegate to your ViewController so you can send data.

有一个适合您的示例;

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let yourViewController = storyboard.instantiateViewController(withIdentifier: "yourViewControllerStoryboardIdentifier") as! YourViewControllerClassName
        yourViewController.yourProperty = "yourValue"
        self.window?.rootViewController = yourViewController
        self.window?.makeKeyAndVisible()
        return true
    }

您可以在Main.storyboard中设置Storyboard ID.只需选择您的ViewController并设置情节提要ID部分,然后点击使用情节提要ID 复选标记(如图片)即可.

You can set your Storyboard ID in your Main.storyboard. Just select your ViewController and set storyboard ID section click the Use Storyboard ID checkmark like image.

这篇关于AppDelegate Segue替代通行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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