[英]Swift delegate not calling function in parent
I am trying to get a child view controller, which is displayed in a view container at the bottom part of the main, parent View Controller. 我正在尝试获取一个子视图控制器,该子视图控制器显示在主视图父视图控制器底部的视图容器中。 When I press the button in the child, "Button pressed from Basic VC" prints to the console, but "Warm button pressed from main VC" does not.
当我按下子级中的按钮时,“从Basic VC按下按钮”会打印到控制台,但不会“从主VC按下温暖的按钮”。 I need the function that prints that second message to be called in the main View Controller.
我需要在主View Controller中打印第二条消息的函数。 Here is the relevant BasicViewController code:
以下是相关的BasicViewController代码:
protocol BasicVCDelegate: class {
func warmButton()
}
class BasicViewController: UIViewController {
weak var BasicVCDelegate: BasicVCDelegate?
@IBOutlet weak var warmButton: UIButton!
@IBAction func warmButton(sender: AnyObject) {
print("Button pressed from Basic VC")
BasicVCDelegate?.warmButton()
}
}
And from ViewController.swift 并从ViewController.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, BasicVCDelegate {
override func viewDidLoad() {
pauseButton.isEnabled = false
hideAll();
self.basicContainer.isUserInteractionEnabled = true;
self.basicContainer.isHidden = false;
self.timerLabel.text = String("00:00:00");
eventTable.dataSource = self
eventTable.delegate = self
loadEvents(event: "timer start")
super.viewDidLoad()
}
// CREATE SEGUEs
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nav = segue.destination as? UINavigationController, let classBasicVC = nav.topViewController as? BasicViewController {
classBasicVC.BasicVCDelegate = self
}
}
// FUNCTIONS FOR BASIC VIEW CONTROLLER TO REFERENCE
func warmButton() {
print("Warm button pressed from main VC")
}
}
EDIT: From everyone below, it seems that the following section of code in ViewController is causing the issue (the line within the if statement is never being called): 编辑:从下面的每个人,似乎在ViewController中的代码的以下部分引起问题(if语句内的行永远不会被调用):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nav = segue.destination as? UINavigationController, let classBasicVC = nav.topViewController as? BasicViewController {
classBasicVC.basicVCDelegate = self
}
}
This is probably due to my unfamiliarity with delegates and the prepareforsegue function. 这可能是由于我不熟悉代理人和prepareforsegue功能。 Need help figuring out what should follow that "if let"
需要帮助弄清楚“ if let”应该遵循的内容
EDIT 2: Replaced the above code with the following as per some users' suggestion (still not reaching the print statement): 编辑2:根据某些用户的建议,将以下代码替换为以下代码(仍未到达打印语句):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nav = segue.destination as? BasicViewController, let classBasicVC = nav.basicVCDelegate {
print("got here!!")
}
}
FINAL EDIT: Thank you for your help! 最后编辑:谢谢您的帮助! After some playing around, the following works (aside from a warning that changes to an error when I address it - I can live with a warning).
经过一番摸索后,以下工作(除了警告,在我解决该错误时会变为错误-我可以接受警告)。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nav = segue.destination as? BasicViewController, let classBasicVC = nav as? BasicViewController? {
classBasicVC?.basicVCDelegate = self
}
}
It should be this instead of your last edited code: 应该是这个,而不是您上一次编辑的代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let classBasicVC = segue.destination as? BasicViewController {
classBasicVC.basicVCDelegate = self
}
}
You haven't assigned button's delegate to the ViewController 您尚未将按钮的委托分配给ViewController
Add this to your ViewController's viewDidLoad
将此添加到您的ViewController的
viewDidLoad
override func viewDidLoad() {
//other code
yourButton.BasicVCDelegate = self
}
Also its a good practice to use camel case for delegate variable like this basicVCDDelegate
对这样的委托变量使用驼峰式大小写也是一种很好的做法,例如
basicVCDDelegate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.