1

简短的 :

我已经实现了Soroush 的Coordinators架构。除了删除以前(子)协调员所需的删除部分外,一切正常。

设想 :

我有两个ViewController名为HomeViewControllerMyGroupsViewController。每个都有自己的协调器,分别命名为HomeCoordinatorMyGroupsCoordinator

用户点击一个按钮HomeViewController触发gotoMyGroupsTapped功能并让用户访问MyGroupsViewController,然后用户点击另一个按钮,通过触发MyGroupsViewController让用户返回。HomeViewControllergotoHomePage()

很简单!: HomeVC -> MyGroupsVC -> HomeVC

但问题是:

navigationController.transitionCoordinator?两个协调员都为零func navigationController(..., didShow viewController: UIViewController...)我无法在每次过渡中删除子协调员。

设置两个协调器navigationController.delegate = self的功能是否正确?start()

我应该navigationController?.popViewController(animated: false )在我的backToHomePage()函数中使用吗?因为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()
     }
4

1 回答 1

0

在我看来,这里的协调器模式使用存在混淆。

从您的预期流程来看HomeVC -> MyGroupsVC -> HomeVC,如果您的意思是从某种意义上说level1 -> level2 -> level3,那么应该使用自己的 newGroupsCoordinator创建一个新实例。HomeCoordinatorHomeVC

所以而不是你以前的代码

class MyGroupViewController ... {
    @IBAction func gotoHomePage(_ sender: Any) {
        if let coord = coordinator as? GroupsCoordinator {
            coord.parentCoordinator?.start()
        }
    }
}

我会把它改成

class MyGroupViewController ... {
    @IBAction func gotoHomePage(_ sender: Any) {
        if let coord = coordinator as? GroupsCoordinator {
            coord.goToHome()
        }
    }
}

class MyGroupsCoordinator ... {

    func goToHome() {
        let homeCoordinator = HomeCoordinator(navigationController: navigationController)
        childCoordinators.append(homeCoordinator)
        groupsCoordinator.parentCoordinator = self
        groupsCoordinator.start()
    }
}

这将允许您创建一个全新的页面,如您所描述的那样HomeVC -> MyGroupsVC -> HomeVC


但是,如果您的意思是采用这种方法level1 -> level2 -> (back) level1,那么您需要在返回时终止MyGroupsCoordinator并从父级中删除。

正如您所注意到的,为此,您需要使用UINavigationControllerDelegate能够在用户导航返回时收到通知(弹出代码或使用经典的后退按钮)。

我发现的一个解决方案是在从其中删除Routera 时使用 a 来处理所有这些导航UIViewController,同时通过闭包通知要删除的正确协调器。你可以在这里阅读更多关于它的信息。

希望能帮助到你

于 2019-10-09T10:38:19.013 回答