1

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

8
  • As you do not add VC3.view as subview, I suspect you have two ViewController3 instances. On as your property in ViewController2 (which you do not see) and one which we do not see the code for. Maybe instantiated by Storyboard. ViewController2s btnCloseAction is called, but its delegateis not set. Commented Jul 3, 2017 at 10:22
  • @shallowThought what is the solution i had tried many alternatives but it is not working. Commented Jul 3, 2017 at 10:25
  • Did you understand my suspect? Commented Jul 3, 2017 at 10:27
  • nope.i am not getting i am having tabbar controller with 3 controller, when i click on second tab a view appears and close button action current view controller gets dissmissed and want to change lable of visible view controller.for all 3 controller i am using xib Commented Jul 3, 2017 at 10:30
  • You are creating a ViewController3 here: let VC3 = ViewController3(nibName: "ViewController3", bundle: nil). But you never use it. Thus you can not see it. As you can not see it, you can not press a button on it. Delete the complete line to find out that it does not change the behaviour of your app in any way. Your posted code does not show the ViewController3 that you actually see, so we can not help with that without more information. Commented Jul 3, 2017 at 10:35

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for suggestion but with my case its different i am having tabbar controller with 3 tabs so its not working, if you know how to work with delegate with tabbar please do suggest me some solution
so you want to pass data between tabs ?
yes, its like applying filter, i am having tabbar controller with 3 tabs, first and third tab contain data on which filter can be applied from tab 2 on clicking tab 2 a view is presented to show filters options based on filter data should get filtered it can be first view or third view it does not matter, i have tried working with delegate on tabbar controller it is working i dont know the reason why it is not working with other controller
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.