Igor Kuznetsov
Igor Kuznetsov

Reputation: 108

Delegate multiple view controllers

I have main view controller which has connection to 2 view controller's. I made protocol where I have didRecive(data:Data) delegate function.

protocol MainViewControllerDelegate: class {
func didReciveDepartment(response:DepartmentResponse)

}

In main view controller I declare delegate var.

    weak var delegate: DepartmentMainViewControllerDelegate?

In prepare for segue I set this delegate to viewCotnroller's. Like so -

        if segue.identifier == "productsEmbedded" {
        let vc = segue.destination as! DepartmentProductsViewController
        delegate = vc


    }
    if segue.identifier == "shopsEmbedded" {
        let vc = segue.destination as! DepartmentShopsViewController
        vc.delegate = self
        delegate = vc
    }

I have wired behavior delegate only triggers in DepartmentShopsViewController, and DepartmentProductsViewController can't get this delegate, I commented out shops and products got this delgate so it means I can't use same delegate for 2 controllers?

Upvotes: 2

Views: 5557

Answers (3)

luckyShubhra
luckyShubhra

Reputation: 2751

These are many ways to pass messages between objects. You can pass data using Delegate and using NSNotificationCentre. The main difference is with delegates, one designated object receives a message, wheras any number of objects can receive notifications when they are posted. You can check this previous SO ques for details.

Upvotes: 2

Nikhlesh Bagdiya
Nikhlesh Bagdiya

Reputation: 4466

For Delegate you have to implement like that

//Declare protocol
protocol MainViewControllerDelegate {
    func didReciveDepartment(response:DepartmentResponse)
}

class DepartmentProductsViewController: UIViewController {

    // MARK: - Variable Declaration.
    var delegate: MainViewControllerDelegate?

}

class DepartmentShopsViewController: UIViewController {

    // MARK: - Variable Declaration.
    var delegate: MainViewControllerDelegate?

}

class  MainViewController: UIViewController, MainViewControllerDelegate {

    //Push ViewController with segue and  use delegate property
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "productsEmbedded"{

            let vc = segue.destination as! DepartmentProductsViewController
            vc.delegate = self
        } else if segue.identifier == "shopsEmbedded" {

            let vc = segue.destination as! DepartmentShopsViewController
            vc.delegate = self
        }
    }

    //MARK: Implement MainViewControllerDelegate Methods.
    func didReciveDepartment(response: DepartmentResponse) {

    }

}

Upvotes: 0

radkrish
radkrish

Reputation: 177

Come on man you forgotten a line

    vc.delegate = self        

The code should look like this

if segue.identifier == "productsEmbedded" {
    let vc = segue.destination as! DepartmentProductsViewController
    vc.delegate = self     //Add this Line   
    delegate = vc
}
if segue.identifier == "shopsEmbedded" {
    let vc = segue.destination as! DepartmentShopsViewController
    vc.delegate = self
    delegate = vc
}

Upvotes: 0

Related Questions