我正在尝试将currentVC设置为通过UINavigationController.导航到的destinationVC的委托
我不能将当前的VC设置为委托,因为prepareForSegue永远不会被触发,而且提供的其他解决方案也不能工作(下面的代码)。
这一切都被设置为Interface-builder的故事板
这是一个体系结构:
-> UITabBarController
-> currentVC (设置为委托)->UINavigationController
-> destinationVC
这没什么用:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print ("The Segue was triggered")
let destinationVC = segue.destination as! MyViewController
destinationVC.delegate = self
}我也不能让它起作用(它从来不通过IF语句):
override func viewDidLoad() {
super.viewDidLoad()
if let myDestinationVC = (self.tabBarController?.viewControllers![0] as? UINavigationController)?.viewControllers[0] as? destinationVC {
print ("The IF statement was triggered")
myDestinationVC.delegate = self
}
}我为我的TabBarController提供了一个自定义类,它现在什么也不做--我不确定是否需要在上面的代码中引用它?
发布于 2019-12-18 23:31:20
下面是一个工作和测试的实现。这不是实现这一目标的最佳方法,但你所描述的一切都会奏效。
class MyTabBarViewController: UITabBarController, UITabBarControllerDelegate {
// Replace with your sending view controller class's type
var sendingViewController: SendingViewController?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Iterate all view controllers to make sure they are instantiated and
// get reference to the sendingViewController
viewControllers?.forEach {
if let navigationController = $0 as? UINavigationController {
// Replace with the type of your sending view controller
if let sendingViewController = navigationController.topViewController as? SendingViewController {
self.sendingViewController = sendingViewController
}
}
}
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let navigationController = viewController as? UINavigationController {
// Replace with the type of your receiving view controller
if let receivingViewController = navigationController.topViewController as? ReceivingViewController,
let sendingViewController = sendingViewController {
// Perform actions here
receivingViewController.view.backgroundColor = sendingViewController.view.backgroundColor
}
}
}
}https://stackoverflow.com/questions/59394204
复制相似问题