我已經在兩個視圖控制器內做了委託協議。但委託方法不會調用我的代碼片段。這是什麼原因。我無法找到問題,請發佈您的建議來重溫此問題。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