Home > Mobile >  Swift Pass event from parent to child
Swift Pass event from parent to child

Time:06-15

I have 2 classes i.e. ViewController and BaseViewController

we have done with inheritance and i have question to pass event from Parent to child not child to parent.

class BaseViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {
    func openCamera() {
       // open UIImagePickerController 
    }

    // Delegates Methods of Image-picker controller
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
        ***My Question: //Pass Event to child class***
    }
}

class ViewController: BaseViewController {

    @IBAction func cameraButtonClicked(_sender: Any) {
        self.openCamera()
    }
}

THanks In advance.

CodePudding user response:

Suppose the event that you need to send picked image from the parent to the child , then you can try

class BaseViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {

    func openCamera() {
       // open UIImagePickerController
    }

    // Delegates Methods of Image-picker controller
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
       sendImage(.....   
    }

    func sendImage(_ image:UIImage) { 
        
    }
}

class ViewController: BaseViewController {

    @IBAction func cameraButtonClicked(_sender: Any) {
        self.openCamera()
    }
    
    override func sendImage(_ image:UIImage) { // override it here
        
    }
}
  • Related