Delegate using Container View in Swift

后端 未结 2 451
故里飘歌
故里飘歌 2021-02-05 23:38

I\'m developing an app for iPad Pro. In this app, containerView use to add additional views and interact with them.

First, I created a protocol

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 00:23

    Like @nwales said you haven't yet set the delegate. You should do set the delegate in prepareForSegue function on your first viewController (who contain the viewContainer)

    First select the embed segue and set an identifier in the attributes inspector. Then in the parentViewController implement the func prepareForSegue like this:

    Swift 4+:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if (segue.identifier == "the identifier") {
                let embedVC = segue.destination as! ViewController
                embedVC.delegate = self
            }
        }
    

    Below:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if (segue.identifier == "the identifier") {
            let embedVC = segue.destinationViewController as! ContainerViewController
            embedVC.dataViewDelegate = self
        }
    }
    

提交回复
热议问题