我正在开发一个应用程序,需要在view和containerView之间传递数据.我需要从两个视图发送数据和接收数据.
让我解释一下:
我可以通过协议更改Label Master(触摸容器按钮),但我无法更改标签容器(触摸主按钮).发生的事情是Master通过以下方式与容器连接.但是没有跟随Container链接到Master.
我试图添加但是segue,但它有效.
主视图控制器:
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
在主视图控制器中保留一个引用.
也就是说,您应该添加一个实例变量来Master
保存对视图控制器的引用,而不仅仅是视图.您需要将其设置为prepareForSegue
.
所以Master View Controller的开头看起来像这样:
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 } }
然后在按钮功能中,只需使用刚添加的变量更改标签即可.
例:
@IBAction func button_Container(sender: AnyObject) { containerViewController?.changeLabel("Nice! It's work!") }
这意味着你也可以摆脱你的MasterToContainer
协议.
我测试了这段代码,所以我知道它有效,但不幸的是我是一个Objective-C开发人员,并且对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:
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
Class ViewController : UIViewController { func sendData(MyStringToSend : String) { let CVC = childViewControllers.last as! ContainerViewController CVC.ChangeLabel( MyStringToSend) } }
在您的ContainerViewController中
Class ContainerViewController : UIViewController { @IBOutlet weak var myLabel: UILabel! func ChangeLabel(labelToChange : String){ myLabel.text = labelToChange } }
从ContainerViewController发送数据 - > ViewController
Class ContainerViewController : UIViewController { func sendDataToVc(myString : String) { let Vc = parentViewController as! ViewController Vc.dataFromContainer(myString) } }
并在ViewController中
Class ViewController : UIViewController { func dataFromContainer(containerData : String){ print(containerData) } }
我希望这会对某人有所帮助.