Swift 3.0 Delegate Protocol doesn't work

2019-07-27 02:48发布

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

}

5条回答
ら.Afraid
2楼-- · 2019-07-27 03:39
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)
查看更多
\"骚年 ilove
3楼-- · 2019-07-27 03:41

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)
查看更多
在下西门庆
4楼-- · 2019-07-27 03:43

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楼-- · 2019-07-27 03:47

Update these changes in viewdidLoad() method

override func viewDidLoad() {
    super.viewDidLoad()

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

}
查看更多
够拽才男人
6楼-- · 2019-07-27 03:47

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)
}
查看更多
登录 后发表回答