chetan panchal
chetan panchal

Reputation: 181

Custom protocol delegate method not calling

I am trying to change lable of one view controller from another view controller using custom protocol but its delegate method is not being called

ViewController3 code:

when i click on close button it's delegate method is not being called in my ViewController2.

protocol ViewController3Delegate: class {

  func changeLable(_ text: String)
}

class ViewController3: UIViewController {

  weak var delegate: ViewController3Delegate?

  override func viewDidLoad() {
    super.viewDidLoad()

  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }

  @IBAction func btnCloseAction(_ sender: Any) {

    delegate?.changeLable("fillter applied")
    self.dismiss(animated: true, completion: nil)
  }
}

ViewController2 code:

class ViewController2: UIViewController,ViewController3Delegate {

  @IBOutlet weak var lblReport: UILabel!

  let VC3 = ViewController3(nibName: "ViewController3", bundle: nil)

  override func viewDidLoad() {

    super.viewDidLoad()
    VC3.delegate = self
  }

  func changeLable(_ text: String) {

    print("delegate called")
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }
}

Anybody knows where i am wrong, please suggest me some solution

Upvotes: 1

Views: 875

Answers (2)

Samarth Kejriwal
Samarth Kejriwal

Reputation: 1176

You have not defined a delegate in your ViewController2 which will be used to the delegate in ViewController3 .

See This :

 protocol ViewController3Delegate: class {

      func changeLable(_ text: String)
    }

    class ViewController3: UIViewController {

      weak var delegate: ViewController3Delegate?

      override func viewDidLoad() {
        super.viewDidLoad()

      }

      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

      }

      @IBAction func btnCloseAction(_ sender: Any) {
        if delgate !=nil {

        delegate?.changeLable("fillter applied")
        self.dismiss(animated: true, completion: nil)
    }
      }
    }

And then in your ViewController2 class :

    class ViewController2: UIViewController,ViewController3Delegate {

      @IBOutlet weak var lblReport: UILabel!


      override func viewDidLoad() {

        super.viewDidLoad()
      }

      func changeLable(_ text: String) {

        print("delegate called")
      }

      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

      }
          override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
             {

                 if segue.identifier = "Yoursegueientifier"
                  {
                     let vc = segue.destination as! ViewController3
                     vc.delegate = self
                  }                
            }
    }

Note : You have to define your segueidentifer name in your storyboard

Upvotes: 2

Aleix R.
Aleix R.

Reputation: 1

You should first present ViewController3 from ViewController2 like this:

let VC3 = ViewController3(nibName: "ViewController3", bundle: nil)
VC3.delegate = self
self.presentViewController(VC3, animated: true, completion: nil)

Also, you can delete this line above viewDidLoad

let VC3 = ViewController3(nibName: "ViewController3", bundle: nil)

Upvotes: 0

Related Questions