我已经在两个视图控制器内做了委托协议。但委托方法不会调用我的代码片段。这是什么原因。我无法找到问题,请发布您的建议来重温此问题。Swift 3.0委托协议不起作用
主视图控制器
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)
}
}
副视点控制器
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!")
}
}
不是'vw.delegateTest = self'是'push.delegateTest = self',不需要这个'self.navigationController?.pushViewController(push!,animated:true)' –
你正在制作testViewController()的对象。为什么需要它。不需要这行只是让push = self.storyboard?.instantiateViewController(withIdentifier:“testViewController”)as! testViewController push.delegateTest = self self.navigationController?.pushViewController(push !, animated:true) –
@ Anbu.Karthik感谢您的支持。我现在改了它。它运作良好。 – Raja