你如何在 Swift 中的视图控制器和其他对象之间共享数据? [英] How do you share data between view controllers and other objects in Swift?

查看:19
本文介绍了你如何在 Swift 中的视图控制器和其他对象之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的 Swift 应用中有多个视图控制器,我希望能够在它们之间传递数据.如果我在视图控制器堆栈中向下几个级别,我如何将数据传递给另一个视图控制器?还是在选项卡栏视图控制器中的选项卡之间?

Say I have multiple view controllers in my Swift app and I want to be able to pass data between them. If I'm several levels down in a view controller stack, how do I pass data to another view controller? Or between tabs in a tab bar view controller?

(注意,这个问题是一个铃声".)问得太多,我决定写一篇关于这个主题的教程.见下面我的回答.

(Note, this question is a "ringer".) It gets asked so much that I decided to write a tutorial on the subject. See my answer below.

推荐答案

您的问题非常.建议对每种情况都有一个简单的包罗万象的解决方案有点幼稚.那么,让我们来看看其中的一些场景.

Your question is very broad. To suggest there is one simple catch-all solution to every scenario is a little naïve. So, let's go through some of these scenarios.

根据我的经验,在 Stack Overflow 上被问到的最常见的场景是从一个视图控制器到下一个视图控制器的简单传递信息.

The most common scenario asked about on Stack Overflow in my experience is the simple passing information from one view controller to the next.

如果我们使用故事板,我们的第一个视图控制器可以覆盖 prepareForSegue,这正是它的用途.调用此方法时会传入一个 UIStoryboardSegue 对象,它包含对目标视图控制器的引用.在这里,我们可以设置我们想要传递的值.

If we're using storyboard, our first view controller can override prepareForSegue, which is exactly what it's there for. A UIStoryboardSegue object is passed in when this method is called, and it contains a reference to our destination view controller. Here, we can set the values we want to pass.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "MySegueID" {
        if let destination = segue.destination as? SecondController {
            destination.myInformation = self.myInformation
        }
    }
}

或者,如果我们不使用故事板,那么我们将从笔尖加载我们的视图控制器.那么我们的代码稍微简单一些.

Alternatively, if we're not using storyboards, then we're loading our view controller from a nib. Our code is slightly simpler then.

func showNextController() {
    let destination = SecondController(nibName: "SecondController", bundle: nil)
    destination.myInformation = self.myInformation
    show(destination, sender: self)
}

在这两种情况下,myInformation 是每个视图控制器上的一个属性,保存需要从一个视图控制器传递到下一个视图控制器的任何数据.它们显然不必在每个控制器上都具有相同的名称.

In both cases, myInformation is a property on each view controller holding whatever data needs to be passed from one view controller to the next. They obviously don't have to have the same name on each controller.

我们可能还想在 UITabBarController 中的选项卡之间共享信息.

We might also want to share information between tabs in a UITabBarController.

在这种情况下,它实际上可能更简单.

In this case, it's actually potentially even simpler.

首先,让我们创建一个 UITabBarController 的子类,并为它提供我们想要在各个选项卡之间共享的任何信息的属性:

First, let's create a subclass of UITabBarController, and give it properties for whatever information we want to share between the various tabs:

class MyCustomTabController: UITabBarController {
    var myInformation: [String: AnyObject]?
}

现在,如果我们从故事板构建我们的应用程序,我们只需将标签栏控制器的类从默认的 UITabBarController 更改为 MyCustomTabController.如果我们不使用故事板,我们只需实例化这个自定义类的实例,而不是默认的 UITabBarController 类,然后将我们的视图控制器添加到它.

Now, if we're building our app from the storyboard, we simply change our tab bar controller's class from the default UITabBarController to MyCustomTabController. If we're not using a storyboard, we simply instantiate an instance of this custom class rather than the default UITabBarController class and add our view controller to this.

现在,标签栏控制器中的所有视图控制器都可以访问此属性:

Now, all of our view controllers within the tab bar controller can access this property as such:

if let tbc = self.tabBarController as? MyCustomTabController {
    // do something with tbc.myInformation
}

并且通过以相同的方式子类化 UINavigationController,我们可以采用相同的方法在整个导航堆栈中共享数据:

And by subclassing UINavigationController in the same way, we can take the same approach to share data across an entire navigation stack:

if let nc = self.navigationController as? MyCustomNavController {
    // do something with nc.myInformation
}

<小时>

还有其他几种情况.这个答案绝不包括所有这些.


There are several other scenarios. By no means does this answer cover all of them.

这篇关于你如何在 Swift 中的视图控制器和其他对象之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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