我试图使用自定义的协议,但它的委托方法不会被调用自定义协议的委托方法不是要求
ViewController3代码从另一个视图控制器改变一个视图控制器的图标:
当我点击关闭按钮它的委托方法不会在我的ViewController2中被调用。
protocol ViewController3Delegate: class {
func changeLable(_ text: String)
}
class ViewController3: UIViewController {
weak var delegate: ViewController3Delegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func btnCloseAction(_ sender: Any) {
delegate?.changeLable("fillter applied")
self.dismiss(animated: true, completion: nil)
}
}
ViewController2代码:
class ViewController2: UIViewController,ViewController3Delegate {
@IBOutlet weak var lblReport: UILabel!
let VC3 = ViewController3(nibName: "ViewController3", bundle: nil)
override func viewDidLoad() {
super.viewDidLoad()
VC3.delegate = self
}
func changeLable(_ text: String) {
print("delegate called")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
任何人都知道在哪里我错了,请建议我一些解决方案
来定义segueidentifer名称作为不添加'VC3.view'作为子视图,我怀疑你有两个'ViewController3'实例。作为你在'ViewController2'中的属性(你没有看到)以及我们没有看到代码的属性。也许由Storyboard实例化。 'ViewController2's'btnCloseAction'被调用,但它的'delegate'没有被设置。 – shallowThought
@shallowThought什么是我尝试了很多替代方案,但它不工作的解决方案。 –
你了解我的嫌疑人吗? – shallowThought