Swift; delegate embedded view controller and paren

2019-05-07 14:44发布

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

EmbeddedController:

protocol ControllerDelegate {
    func hideContainerView()
}

class EmbeddedController: UIViewController {
    var delegate: VControllerDelegate?

    @IBAction func button(sender: AnyObject) {
    delegate?.hideContainerView()
    }
}

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?

2条回答
再贱就再见
2楼-- · 2019-05-07 15:14

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:

        let vc = EmbeddedController()
        vc.delegate = self
        containerView.addSubview(vc.view)
        self.addChildViewController(vc)
        vc.didMoveToParentViewController(self)
查看更多
Rolldiameter
3楼-- · 2019-05-07 15:19

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
    }
}

In storyboard I selected the segue from the MainController to the EmbeddedController, and set the identifier to "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. 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 :)

查看更多
登录 后发表回答