mrdb10
mrdb10

Reputation: 1

How to make container ViewController conform to a custom protocol without having access to embed segue?

I am trying to embed a ViewController of type UITableViewController into another view controller, and so far I have done this part correctly. I have a custom protocol that has a method that must be accessed from the TableViewController class. Now this method in this protocol must also be set inside the HomeViewController(that one that contains the container view) so that by the time the TableViewController class attempts to access that method, the method is not nil.

I am trying to duplicate something that was done by a book I am currently following, The Big Nerd Ranch Guide iOS programming 7th Edition. In the example of the book, the author does what I just described but he uses an actual embed segue reference from storyboard but that's not possible for me since I have already deleted storyboard and doing this entirely in code. Here's the example from the author.

var moodsConfigurable: MoodsConfigurable!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier {
        case “embedContainerViewController”:
            guard let moodsConfigurable = segue.destination as? MoodsConfigurable else {
                preconditionFailure(
                    “View controller expected to conform to MoodsConfigurable”)
            }
            self.moodsConfigurable = moodsConfigurable
            segue.destination.additionalSafeAreaInsets =
            UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0)
        default:
            preconditionFailure(“Unexpected segue identifier”)
    }
}

As you can probably tell, the author checks to see if the homeviewcontroller conforms to the custom protocol before it sets the property set in the first line. How do I do this since I have no segue?

I tried the following:

guard let moodsConfigurable = HomeViewcontroller() as? MoodsConfigurable else {
    print("Problem, exiting program here")
    return
}

This isn't working.

Upvotes: 0

Views: 37

Answers (0)

Related Questions