2

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?

Igor Kuznetsov
  • 108
  • 1
  • 10

3 Answers3

2

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.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
  • Wrong, those 2 view controllers connected to main as container's, and I use preapreForSegue only one time, when MainViewController is inited. There I setting those delegates, and delegates from those viewcontroller's to main controller to establishes communication between them. – Igor Kuznetsov Jul 26 '17 at 10:33
  • Clarify me how you get data in your MainViewController ?? – luckyShubhra Jul 26 '17 at 10:40
  • I pass it through preapreForSegue from previous controller which present MainViewController, and later one I just update this data from function inside MainViewController. – Igor Kuznetsov Jul 26 '17 at 10:43
  • can you show me where you have defined protocol DepartmentMainViewControllerDelegate ?? – luckyShubhra Jul 26 '17 at 10:43
  • check question I just removed Department letter. It's defined in DepartmentMainViewController class. – Igor Kuznetsov Jul 26 '17 at 10:44
  • Btw if u need to update data from one view controller to 2 view controller. Its not feasible. Delegate is for one to one connection. For one to many NSNotificationCentre should be used. – luckyShubhra Jul 26 '17 at 10:48
  • I want avoid NotificaitonCenter, what also I can use? – Igor Kuznetsov Jul 26 '17 at 11:09
0

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
}
radkrish
  • 130
  • 1
  • 15
  • Wrong, check question I have all this thing's delegate triggers in one controller. and vc.delgate on DepartmentsShops because I have DepartmentShopsDelegatei it's a different story. – Igor Kuznetsov Jul 26 '17 at 10:16
0

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) {

    }

}
Nikhlesh Bagdiya
  • 4,316
  • 1
  • 19
  • 29