How do you share data between view controllers and other objects in Swift?(如何在 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
}
}
}
或者,如果我们不使用情节提要,那么我们将从 nib 加载我们的视图控制器.那么我们的代码会稍微简单一些.
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 中的视图控制器和其他对象之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Swift 中的视图控制器和其他对象之间共享数据?


- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01