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

查看:78
本文介绍了如何在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.

如果我们使用的是storyboard,我们的第一个视图控制器可以覆盖 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 prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "MySegueID" {
        if let destination = segue.destinationViewController 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: NSBundle.mainBundle())
    destination.myInformation = self.myInformation
    self.showViewController(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天全站免登陆