Swift 3.0委托协议不起作用
我已经在两个视图控制器内做了委托协议。但委托方法不会调用我的代码片段。这是什么原因。我无法找到问题,请发布您的建议来重温此问题。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!")
}
}
更新这些变化viewdidLoad()
方法
override func viewDidLoad() {
super.viewDidLoad()
if let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as? SelectionScreen
{
push.delegateTest = self
}
}
这个'self.navigationController的使用是什么?.pushViewController(push!,animated:true)' –
sry刚刚编辑答案 –
其确定我的兄弟,...答案更新为更安全的句柄 –
你AR问题Ë面对由于这一行
let vw = testViewController()
vw.delegateTest = self
您已经创建的实例testViewController的大众,并分配大众实例
的代表在下一行
let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController")
self.navigationController?.pushViewController(push!, animated: true)
您正在创建的不同实例testviewcontroller push
还有无需此代码,
let vw = testViewController()
vw.delegateTest = self
而是做
let push testViewController = self.storyboard.instantiateViewController(withIdentifier: "testViewController") as! testViewController
push.delegateTest = self
self.navigationController?.pushViewController(push, animated: true)
没有类的类型它不允许你设置'delegateTest'所以你需要写let let = self.storyboard ?.instantiateViewController(withIdentifier:“testViewController”)as! testViewController –
@NimishaRanipa - 谢谢。我正在更新答案 –
@Dev_Tandel非常感谢。它的工作现在。 – Raja
let vw = testViewController() // Your are doing wrong code
vw.delegateTest = self
只能使用此
我希望它会为你工作
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let push = storyboard.instantiateViewController(withIdentifier:"testViewController")as! testViewController
push.delegateTest = self
self.navigationController?.pushViewController(push, animated: true)
非常感谢。它的工作现在。 – Raja
这种合作de snippet没有意义。您只需创建一个对象,但不会进一步使用它。所以删除这段代码片段。
let vw = testViewController()
vw.delegateTest = self
而且这样做:
override func viewDidLoad() {
super.viewDidLoad()
let pushVC = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as! testViewController
pushVC.delegateTest = self
self.navigationController?.pushViewController(pushVC, animated: true)
}
非常感谢。它的工作现在。 – Raja
我觉得,你把代码func testMethod(value:String) { print("Hai", value) }
到viewDidLoad中和上述let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") self.navigationController?.pushViewController(push!, animated: true)
感谢您的支持。我的错只是代表的实例 “delegateTest” vw.delegateTest = self 现在我更新了请检查它。 selectionVc.delegateTest = self – Raja
不是'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