[英]Coordinator Pattern to replace UINavigationController in SplitViewController
I am implementing coordinator pattern to handle navigation in my app.我正在实施协调器模式来处理我的应用程序中的导航。 In theory when users choose different category I want to set the splitViewController to replace the existing navigationController for that category by a new one.
理论上,当用户选择不同的类别时,我想设置 splitViewController 以用新的类别替换该类别的现有导航控制器。
When app starts the coordinator operate as expected, and when I pop or push on the same navigationController
implemented at start also works fine, my only problem is when I try to replace the whole navigationController
of the splitviewcontroller
.当应用程序启动协调器按预期运行时,当我弹出或推送在启动时实现的同一个
navigationController
splitviewcontroller
时也可以正常工作,我唯一的问题是当我尝试替换splitviewcontroller
的整个navigationController
splitviewcontroller
。
ISSUE: adding new navigationController is not displayed to the user问题:添加新的 navigationController 不会显示给用户
here is my implementation.这是我的实现。
class Coordinator: Navigable, DataCommunicator{
//MARK: - Navigable Conformable
typealias UIController = SplitController
var viewController: UIController
var childCoordinators: [Coordinatable] = []
//MARK: - Root Custom setup
weak var parentCoordinator: RootCoordinator?
//MARK: - Init
init(viewController: UIController) {
self.viewController = viewController
}
func start() {
let categoryNavigationController = CategoryNavigationController()
let categoryNavigationCoordinator = CategoryNavigationCoordinator(viewController: noteNavigationController)
categoryNavigationCoordinator.start()
childCoordinators.append(categoryNavigationCoordinator)
categoryNavigationController.coordinator = self
viewController.viewControllers = [categoryNavigationController]
}
func startSearchCategory() {
childCoordinators.removeLast()
viewController.navigationController?.popToRootViewController(animated: false)
viewController.viewControllers.removeLast()
let searchNavigationController = SearchNavigationController()
let searchCoordinator = SearchNavigationCoordinator(viewController:searchNavigationController)
searchCoordinator.start()
childCoordinators.append(searchCoordinator)
searchNavigationController.coordinator = self
searchCoordinator.parentCoordinator = self
viewController.viewControllers = [searchNavigationController]
}
}
Update: I think I reached the desired behavior with a different approach, still I am curious why I can't display different navigationController
for the masterController in the UISplitViewController
and display it.更新:我想我用不同的方法达到了所需的行为,但我仍然很好奇为什么我不能在
UISplitViewController
中为UISplitViewController
显示不同的navigationController
并显示它。
But my approach helped my code to be more modular.但是我的方法帮助我的代码更加模块化。 I added in my
Coordinator
protocol the following function我在我的
Coordinator
协议中添加了以下功能
func stopChild<T: Coordinatable>(coordinator: T, callback: CoordinatorCallBack?)
and implemented the function as the following:并实现了如下功能:
override func stopChild<T>(coordinator: T, callback: CoordinatorCallBack?) where T : Coordinatable {
childCoordinators = childCoordinators.filter({$0 !== coordinator})
// Calling parent to stop the child coordinator to roll back to the rootController
parentCoordinator?.stopChild(coordinator: self, callback: nil)
}
Rolling back helped me to instantiate the full stack I desire without trying to add custom modifying code for the splitViewController
, instead I am replacing the whole splitViewController
with the one corresponding to the module I am working with, which is prettier for generic use.回滚帮助我实例化了我想要的完整堆栈,而无需尝试为
splitViewController
添加自定义修改代码,相反,我将整个splitViewController
替换为与我正在使用的模块相对应的那个,这对于通用使用来说更漂亮。 Since in my call back I can send to the root coordinator the desired module the user will be interested in next.因为在我的回调中,我可以将用户接下来感兴趣的所需模块发送给根协调器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.