[英]Swift; delegate embedded view controller and parent
Sorry in advance that I can't explain myself very well. 提前抱歉,我无法解释自己。 I'm really new to programming and the topic of delegation still eludes me.
我对编程很陌生,代表团的主题仍然没有找到我。 I had some great help with this once before, but now I am trying to use a delegate in a different situation and I can't get it right.
我曾经有过一次很好的帮助 ,但现在我试图在不同的情况下使用代表,我无法做到这一点。 I pieced together a bit of code that doesn't work, and no matter how much I search I can't find a way to fix it.
我拼凑了一些不起作用的代码,无论我搜索多少,我找不到修复方法。
I have a view controller (MainController) with and embedded view controller (EmbeddedController) in a container view. 我在容器视图中有一个视图控制器(MainController)和嵌入式视图控制器(EmbeddedController)。 I am trying to have a button in the embedded controller manipulate the container view (containerView).
我试图在嵌入式控制器中有一个按钮来操纵容器视图(containerView)。
EmbeddedController: EmbeddedController:
protocol ControllerDelegate {
func hideContainerView()
}
class EmbeddedController: UIViewController {
var delegate: VControllerDelegate?
@IBAction func button(sender: AnyObject) {
delegate?.hideContainerView()
}
}
MainController: MainController:
class MainController: UIViewController, ControllerDelegate {
@IBOutlet var containerView: UIView!
func hideContainerView() {
containerView.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
var vc = EmbeddedController()
vc.delegate = self
}
}
Does anyone have any idea what I am doing wrong? 有谁知道我做错了什么? And why this isn't working?
为什么这不起作用?
What I ended up doing is adding this to the MainController: 我最终做的是将它添加到MainController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "mySegue") {
let vc = segue.destinationViewController as! EmbeddedController
vc.delegate = self
}
}
In storyboard I selected the segue from the MainController to the EmbeddedController, and set the identifier to "mySegue". 在故事板中,我选择了从MainController到EmbeddedController的segue,并将标识符设置为“mySegue”。
Without the code above the delegate kept returning nil. 如果没有上面的代码,代表将继续返回零。 I didn't look into this solution at first as I thought segues were only for transitioning between view controllers, and in my mind I didn't see the embedded controller as a transition.
我一开始并没有考虑这个解决方案,因为我认为segues仅用于在视图控制器之间进行转换,在我看来,我没有看到嵌入式控制器是一个转换。 Maybe someone more knowledgable than me (which is practically anyone on here at this point) can explain how this is all fitting together.
也许比我更知识渊博的人(此时几乎就是这里的任何人)可以解释这一切是如何融合在一起的。
In any case, this is how I solved my issue and hopefully someone else can benefit from this as well :) 在任何情况下,这就是我解决我的问题的方式,希望其他人也可以从中受益:)
First of all, to avoid strong reference cycles: 首先,要避免强烈的参考周期:
protocol ControllerDelegate: class {
func hideContainerView()
}
class EmbeddedController: UIViewController {
weak var delegate: ControllerDelegate?
And you haven't added your newly instantiated VC view to container view, and not added it as a child VC: 并且您尚未将新实例化的VC视图添加到容器视图,而不是将其添加为子VC:
let vc = EmbeddedController()
vc.delegate = self
containerView.addSubview(vc.view)
self.addChildViewController(vc)
vc.didMoveToParentViewController(self)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.