迅速;委托嵌入式视图控制器和父级 [英] Swift; delegate embedded view controller and parent

查看:73
本文介绍了迅速;委托嵌入式视图控制器和父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我不能很好地解释自己。我真的是编程新手,而委托这个话题仍然困扰着我。我有一些很棒的帮助曾经有过一次,但是现在我试图在其他情况下使用委托,但我做对了。我拼凑了一些无效的代码,无论我搜索了多少代码,都找不到解决它的方法。

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.

我在容器视图中有一个带有嵌入式视图控制器(EmbeddedController)的视图控制器(MainController)。我正在尝试让嵌入式控制器中的按钮操作容器视图(containerView)。

I have a view controller (MainController) with and embedded view controller (EmbeddedController) in a container view. I am trying to have a button in the embedded controller manipulate the container view (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?

推荐答案

我最终要做的是将其添加到MainController中:

What I ended up doing is adding this to the MainController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "mySegue") {
        let vc = segue.destinationViewController as! EmbeddedController
        vc.delegate = self
    }
}

故事板中,我从MainController到EmbeddedController选择了segue,并将标识符设置为 mySegue。

In storyboard I selected the segue from the MainController to the EmbeddedController, and set the identifier to "mySegue".

在委托上方的代码没有返回nil的情况下。起初我没有研究此解决方案,因为我认为segue仅用于在视图控制器之间进行转换,而且在我看来,我并不认为嵌入式控制器是一种过渡。也许比我更有知识的人(这时几乎是这里的任何人)都可以解释这两者如何契合。

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. 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 :)

这篇关于迅速;委托嵌入式视图控制器和父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆