Swift 3.0 Delegate Protocol doesn't work

不问归期 提交于 2020-01-05 04:40:13

问题


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!")
}

}

回答1:


Update these changes in viewdidLoad() method

override func viewDidLoad() {
    super.viewDidLoad()

   if let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as? SelectionScreen
  {
    push.delegateTest = self
  }

}



回答2:


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)



回答3:


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)
}



回答4:


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)




回答5:


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)


来源:https://stackoverflow.com/questions/45815186/swift-3-0-delegate-protocol-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!