Swift 3.0委托协议不起作用 [英] Swift 3.0 Delegate Protocol doesn't work

查看:142
本文介绍了Swift 3.0委托协议不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个视图控制器中创建了委托协议。但委托方法不会调用我的代码段。这是什么原因。我找不到问题,请发表您的建议以重温此问题。

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.

主视图控制器

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控制器

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

}


推荐答案

viewdidLoad()方法中更新这些更改

override func viewDidLoad() {
    super.viewDidLoad()

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

}

这篇关于Swift 3.0委托协议不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆