I have made delegate protocol within two view controllers. but the delegate method doesn't call on my code snippet. what is the reason for that. I couldn't find out the issue kindly post your suggestions to relive this issue.
Main View controller
class ViewController: UIViewController, testDelegateMethod {
override func viewDidLoad() {
super.viewDidLoad()
let vw = testViewController()
vw.delegateTest = self
let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController")
self.navigationController?.pushViewController(push!, animated: true)
}
func testMethod(value:String) {
print("Hai", value)
}
}
Sub View controller
protocol testDelegateMethod {
func testMethod(value:String)
}
class testViewController: UIViewController {
var delegateTest : testDelegateMethod?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func actSubmit(_ sender: Any) {
delegateTest?.testMethod(value: "Hello how are you!")
}
}
Update these changes in viewdidLoad()
method
override func viewDidLoad() {
super.viewDidLoad()
if let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as? SelectionScreen
{
push.delegateTest = self
}
}
The issue you are facing due to this line
let vw = testViewController()
vw.delegateTest = self
You have created instance of testViewController vw, and Assigned delegate of vw instance
In the next line
let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController")
self.navigationController?.pushViewController(push!, animated: true)
You are creating different instance of testviewcontroller push
There is no need of this code,
let vw = testViewController()
vw.delegateTest = self
Instead do
let push testViewController = self.storyboard.instantiateViewController(withIdentifier: "testViewController") as! testViewController
push.delegateTest = self
self.navigationController?.pushViewController(push, animated: true)
This code snippet does not make sense. You just create an object but does not use it further. So remove this code snippet.
let vw = testViewController()
vw.delegateTest = self
And do like this:
override func viewDidLoad() {
super.viewDidLoad()
let pushVC = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as! testViewController
pushVC.delegateTest = self
self.navigationController?.pushViewController(pushVC, animated: true)
}
I think, you put code func testMethod(value:String) {
print("Hai", value)
}
into viewDidLoad and above let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController")
self.navigationController?.pushViewController(push!, animated: true)
let vw = testViewController() // Your are doing wrong code
vw.delegateTest = self
Use only this
I hope it will work for you
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let push = storyboard.instantiateViewController(withIdentifier:"testViewController")as! testViewController
push.delegateTest = self
self.navigationController?.pushViewController(push, animated: true)