在ViewController和ContainerViewController之间传递数据 [英] Pass data between ViewController and ContainerViewController
问题描述
我正在开发一个应用程序,需要在view和containerView之间传递数据.我需要从两个视图发送数据和接收数据.
I'm working on an app, and need to pass data between view and containerView. I need to send data and receive data from both Views.
让我更好地解释一下:
我可以通过协议更改标签主控(触摸容器按钮),但是我不能更改标签容器(触摸主按钮).发生的情况是主服务器通过以下方式与容器连接.但是没有跟随Container链接到Master.
I can change the Label Master (Touch the Container Button) by protocol, but I can not change the Label Container (Touch the Master button). What happens is the Master connects with the container by a following. But do not have a follow Container linking to the Master.
我尝试添加但选择添加,但是有效.
I tried to add but segue to, but it worked.
主视图控制器:
import UIKit
protocol MasterToContainer {
func changeLabel(text:String)
}
class Master: UIViewController, ContainerToMaster {
@IBOutlet var containerView: UIView!
var masterToContainer:MasterToContainer?
@IBOutlet var labelMaster: UILabel!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerViewSegue" {
let view = segue.destinationViewController as? Container
view!.containerToMaster = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func button_Container(sender: AnyObject) {
masterToContainer?.changeLabel("Nice! It's work!")
}
func changeLabel(text: String) {
labelMaster.text = text
}
}
容器视图控制器:
import UIKit
protocol ContainerToMaster {
func changeLabel(text:String)
}
class Container: UIViewController, MasterToContainer {
var containerToMaster:ContainerToMaster?
@IBOutlet var labelContainer: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func button_Master(sender: AnyObject) {
containerToMaster?.changeLabel("Amazing! It's work!")
}
func changeLabel(text: String) {
labelContainer.text = text
}
}
有人可以帮助我吗?
推荐答案
您需要做的就是在主视图控制器中保留对Container
的引用.
All you need to do is keep a reference to Container
in your master view controller.
也就是说,您应该在Master
中添加一个实例变量,该变量将包含对视图控制器的引用,而不仅仅是对视图的引用.您需要在prepareForSegue
中进行设置.
That is, you should add an instance variable to Master
that will hold a reference to the view controller, not just the view. You'll need to set it in prepareForSegue
.
因此,Master View Controller的开始看起来像这样:
So the beginning of Master View Controller would look something like this:
class Master: UIViewController, ContainerToMaster {
@IBOutlet var containerView: UIView!
var containerViewController: Container?
@IBOutlet var labelMaster: UILabel!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerViewSegue" {
containerViewController = segue.destinationViewController as? Container
containerViewController!.containerToMaster = self
}
}
然后在按钮功能中,只需使用刚刚添加的变量更改标签即可.
And then in your button function, simply change the label using the variable you just added.
示例:
@IBAction func button_Container(sender: AnyObject) {
containerViewController?.changeLabel("Nice! It's work!")
}
这意味着您也可以摆脱MasterToContainer
协议.
This means you can get rid of your MasterToContainer
protocol too.
我测试了这段代码,所以我知道它可以工作,但是不幸的是我是一个Objective-C开发人员,对Swift的最佳实践一无所知.因此,我不知道这是否是最好的解决方法,但它确实有效.
I tested this code, so I know it works, but unfortunately I am an Objective-C dev, and know nothing about best practices in Swift. So I don't know if this is the best way to go about it, but it certainly works.
这是我测试过的确切代码:
Here's the exact code I've tested:
Master.swift:
Master.swift:
import UIKit
class Master: UIViewController, ContainerToMaster {
@IBOutlet var containerView: UIView!
@IBOutlet var labelMaster: UILabel!
var containerViewController: Container?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerViewSegue" {
containerViewController = segue.destinationViewController as? Container
containerViewController!.containerToMaster = self
}
}
@IBAction func button_Container(sender: AnyObject) {
containerViewController?.changeLabel("Nice! It's work!")
}
func changeLabel(text: String) {
labelMaster.text = text
}
}
Container.swift:
Container.swift:
import UIKit
protocol ContainerToMaster {
func changeLabel(text:String)
}
class Container: UIViewController {
@IBOutlet var labelContainer: UILabel!
var containerToMaster:ContainerToMaster?
@IBAction func button_Master(sender: AnyObject) {
containerToMaster?.changeLabel("Amazing! It's work!")
}
func changeLabel(text: String) {
labelContainer.text = text
}
}
这篇关于在ViewController和ContainerViewController之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!