如何将数据从一个容器传递到另一个容器,两者都嵌入在 swift 中的同一个 uiviewcontroller 中? [英] how can I pass data from one container to another, both embedded in the same uiviewcontroller in swift?
问题描述
我有一个父 UIViewController
,它有两个不同的视图容器——每个容器都嵌入了 UIViewController
.它看起来像这样:
I have a parent UIViewController
and it has two different view containers - each of them has embedded UIViewController
inside. It looks somehow like this:
当用户按下存储在左侧容器的按钮时,我想更改右侧容器上的标签.
I want to change the label on the right container when user presses the button stored on the left one.
到目前为止,我能够在父视图控制器中放置一个按钮的情况下做到这一点,然后我只是使用了一个协议:
So far I was able to do it while having a button placed in a parent view controller, then I was just using a protocol:
在我的父组件中:
in my parent component I had:
class ParentController: UIViewController {
var delegateEmbedded:HandleEmbedded?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueToFirstEmbeddedController"){
if let embeddedView = segue.destinationViewController as? EmbeddedContainer {
self.delegateEmbedded = embeddedView
}
}
在我的容器嵌入式 UIViewController 中:
in my container-embedded UIViewController I had:
protocol HandleEmbedded: class {
func setName(label: String)
}
class EmbeddedContainer: UITableViewController, HandleYourChat{
func setName(label: String){
print("setting label to (label)")
}
}
当我将按钮放置在父控制器中并想要更改容器内的标签时,上述情况有效.但是,当按钮也被嵌入但在与标签不同的容器中时,会发生什么,我应该如何传递数据?我是否必须通过父控制器传递数据?这样做的最佳方法是什么?
Situation above works when I have the button placed in a parent controller and want to change the label inside a container. But what happens and how should I pass the data when the button is also embedded, but in a different container than the label? Do I have to pass the data through the parent controller? What's the best way for doing so?
推荐答案
要将数据从一个嵌入式 ViewController 传递到另一个嵌入式 ViewController,请让父级处理传输.在这里,我提供了一个完整的示例,其中包含三个 ViewController 和一个 StringTaker
协议.主要的 ViewController
和 LabelViewController
都实现了这个协议.主要的 ViewController
从 ButtonViewController
获取一个字符串并将其传递给嵌入的 LabelViewController
.
To pass data from one embedded ViewController to another embedded ViewController, have the parent handle the transfer. Here I have provided a complete example with three ViewControllers and a single StringTaker
protocol. Both the main ViewController
and the LabelViewController
implement this protocol. The main ViewController
takes a string from the ButtonViewController
and passes it on to the embedded LabelViewController
.
ViewController.swift
import UIKit
protocol StringTaker: class {
func takeString(string: String)
}
class ViewController: UIViewController, StringTaker {
weak var stringTaker: StringTaker?
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EmbedButtonViewController" {
let dvc = segue.destinationViewController as! ButtonViewController
dvc.delegate = self
} else if segue.identifier == "EmbedLabelViewController" {
let dvc = segue.destinationViewController as! LabelViewController
stringTaker = dvc
}
}
// Receive the string from the ButtonViewController
func takeString(string: String) {
// Pass it to the LabelViewController
stringTaker?.takeString(string)
}
}
ButtonViewController.swift
import UIKit
class ButtonViewController: UIViewController {
weak var delegate: StringTaker?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func generateString(sender: UIButton) {
let cities = ["Boston", "Paris", "Sydney", "Mumbai", "Lima"]
// Pick a random city
let city = cities[Int(arc4random_uniform(UInt32(cities.count)))]
// Pass the string to the delegate
delegate?.takeString(city)
}
}
LabelViewController.swift
import UIKit
class LabelViewController: UIViewController, StringTaker {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func takeString(string: String) {
myLabel.text = string
}
}
<小时>
注意事项:
Things to note:
LabelViewController
和ButtonViewController
对使用它们的ViewController
一无所知.这使得重用它们变得更加容易.您可以将它们嵌入到另一个 viewController 中,只要您正确实现了StringTaker
协议并设置了delegate
,一切正常.- 在命名嵌入转场然后在
prepareForSegue
中正确设置代表时,将其连接起来的关键.将 Container 添加到ViewController
后,可以在 Document Outline 视图中找到 segue.
- The
LabelViewController
and theButtonViewController
know nothing about theViewController
that uses them. This makes it easier to reuse them. You could embed them in another viewController and as long as you properly implement theStringTaker
protocol and set up thedelegate
, everything works. - The key to hooking this up in in naming the embed segues and then properly setting up the delegates in
prepareForSegue
. The segues can be found in the Document Outline view once the Container is added to theViewController
.
这篇关于如何将数据从一个容器传递到另一个容器,两者都嵌入在 swift 中的同一个 uiviewcontroller 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!