【发布时间】:2019-07-24 12:12:07
【问题描述】:
简介:
我已经实现了Soroush's 协调器架构。除了删除以前(子)协调员所需的删除部分外,一切正常。
场景:
我有两个ViewController,分别命名为HomeViewController 和MyGroupsViewController。每个都有自己的协调器,分别命名为 HomeCoordinator 和 MyGroupsCoordinator。
用户点击HomeViewController 上的一个按钮,触发gotoMyGroupsTapped 函数并将用户带到MyGroupsViewController,然后用户点击MyGroupsViewController 上的另一个按钮,通过触发@987654333 让用户回到HomeViewController @。
很简单! : HomeVC -> MyGroupsVC -> HomeVC
但问题是:
navigationController.transitionCoordinator? 在两个协调器中的func navigationController(..., didShow viewController: UIViewController...) 中为 nil,我无法在每个转换中删除子协调器。
在两个协调器的start() func 中设置navigationController.delegate = self 是否正确?
我应该在我的backToHomePage() 函数中使用navigationController?.popViewController(animated: false ) 吗?因为Paul Hudson 只使用了pushViewController。
我的代码 [简化版]:
HomeCoordinator.swift
import Foundation
import UIKit
class HomeCoordinator: NSObject,Coordinator,UINavigationControllerDelegate {
var childCoordinators = [Coordinator]()
var navigationController: UINavigationController
weak var parentCoordinator : Coordinator?
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
// Transition here is nil
print(" Transition : ",navigationController.transitionCoordinator)
guard let fromViewController = navigationController.transitionCoordinator?.viewController(forKey: .from) else {
print("Unknown fromViewController!")
return
}
// Removing a child coordinator
}
func gotoMyGroups (){
let groupsCoordinator = GroupsCoordinator(navigationController: navigationController)
childCoordinators.append(groupsCoordinator)
groupsCoordinator.parentCoordinator = self
groupsCoordinator.start()
}
func start() {
let vc = HomeViewController.instantiate()
vc.coordinator = self
navigationController.delegate = self
navigationController.pushViewController(vc, animated: false)
navigationController.setNavigationBarHidden(true, animated: false)
}
}
MyGroupsCoordinator.swift
import Foundation
import UIKit
class MyGroupsCoordinator: NSObject,Coordinator,UINavigationControllerDelegate {
var childCoordinators = [Coordinator]()
var navigationController: UINavigationController
weak var parentCoordinator : Coordinator?
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
// Transition here is nil
print(" Transition : ",navigationController.transitionCoordinator)
guard let fromViewController = navigationController.transitionCoordinator?.viewController(forKey: .from) else {
print("Unknown fromViewController!")
return
}
// Removing a child coordinator
}
func start() {
let vc = MyGroupViewController.instantiate()
vc.coordinator = self
navigationController.delegate = self
navigationController.pushViewController(vc, animated: false)
navigationController.setNavigationBarHidden(true, animated: false)
}
}
MyGroupViewController.magik
class MyGroupViewController : UIViewControllerWithCoordinator,UITextFieldDelegate,Storyboarded{
@IBAction func gotoHomePage(_ sender: Any) {
if let coord = coordinator as? GroupsCoordinator {
coord.parentCoordinator?.start()
}
}
}
HomeViewController.swift
class HomeViewController: UIViewControllerWithCoordinator,Storyboarded {
@IBAction func gotoMyGroupsTapped(_ sender: Any) {
guard let acoordinator = coordinator as? HomeCoordinator else {
return
}
acoordinator.gotoMyGroups()
}
【问题讨论】:
-
你解决了吗?我有类似的问题,我发现当我从子协调器中删除 navigationController.delegate = self 时,子协调器的删除被执行,而不是当子被设置为导航控制器的委托时
标签: swift uinavigationcontroller coordinator-pattern transition-coordinator