AppDelegate segue 替代传递数据 [英] AppDelegate segue alternative pass data

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

问题描述

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

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?

提前致谢.

推荐答案

你不能使用 segue to ViewController from AppDelegate,因为 AppDelegate> 不是 ViewController.Segue 仅适用于 ViewControllers.

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

但是您可以从您的 AppDelegate 向您的 ViewController 启动一个故事板标识符,以便您可以发送数据.

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天全站免登陆