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
VC3.viewas subview, I suspect you have twoViewController3instances. On as your property inViewController2(which you do not see) and one which we do not see the code for. Maybe instantiated by Storyboard.ViewController2sbtnCloseActionis called, but itsdelegateis not set.ViewController3here: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 theViewController3that you actually see, so we can not help with that without more information.