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!")
}
}
Use only this
I hope it will work for you
The issue you are facing due to this line
You have created instance of testViewController vw, and Assigned delegate of vw instance
In the next line
You are creating different instance of testviewcontroller push
There is no need of this code,
Instead do
I think, you put code
func testMethod(value:String) { print("Hai", value) }
into viewDidLoad and abovelet push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") self.navigationController?.pushViewController(push!, animated: true)
Update these changes in
viewdidLoad()
methodThis code snippet does not make sense. You just create an object but does not use it further. So remove this code snippet.
And do like this: