dispatch event to parent ViewController in swift

前端 未结 2 551
感情败类
感情败类 2021-02-04 08:07

I\'m coming from as AS3 background so it might be easier for me to show you what I\'m trying to do with AS3. I have a UIViewController(root) and inside that I have a ContainerV

2条回答
  •  再見小時候
    2021-02-04 08:42

    Well or you can use static variables, which is probably the most straightforward way.

    class ParentViewController: UIViewController {
    
        static var instance:ParentViewController?
    
        override func awakeFromNib() {
            self.dynamicType.instance = self
        }
    
        func parentFunction() {
            print("called \(#function) in \(self.dynamicType)")
        }
    }
    
    class ChildViewController: UIViewController {
        func childFunction() {
            ParentViewController.instance?.parentFunction()
        }
    
        override func viewDidAppear(_ animated: Bool) {
            childFunction()
        }
    }
    

提交回复
热议问题