UITabBarController 如何适应 VIPER 架构?

How a UITabBarController fit into the VIPER architecture?

我正在编写一个具有基于 TabBar 的导航的应用程序。我正在采用 VIPER 架构,但我对 UITabBarController 的选项卡更改应如何实现这一主题感到非常困惑。

我还是 VIPER 的新手,所以我的两分钱可能不值多少钱,但也许可以在 AppDelegate 上将标签栏设为私有 属性。当您需要更改为特定索引时,可以使用实用方法更改标签栏选定的索引,但也会触发 wireframe/router 创建过程。

这可能晚了,但可能对其他人有所帮助。 我的用例是在登录屏幕之后实现 tabBarController。 在 VIPER 中我们可以通过多种方式实现,但我是如何实现的:

  1. 手动分配 TabBar,不使用故事板。
  2. 创建一个新的 WireFrame class 仅用于 TabBarWireframe 展示。
  3. 使用一个可变数组创建一个单例 class,该数组将包含要分配给 tabBarController 的所有视图控制器。
  4. 创建一个 json 文件,它将为我提供选项卡的值,可以跳过此步骤,因为我希望选项卡基于 JSON 文件中的值是动态的。如果您有静态标签,请跳过此步骤。
  5. 在 TabBarWireframe 中,放置一个调用所有选项卡线框的循环。
  6. 在您的个人线框图中,只需实例化 viewController obj 并将其添加到我们在步骤 3 中创建的单例 class 数组。
  7. 毕竟 viewController 是选项卡的一部分,也是数组的一部分。只需从 loginviewcontroller 实例中呈现 tabBar 控制器(它的实例只是通过一个方法传递给 tabBarWireframe class)。

希望我说得有道理。

使用 VIPER 架构实现 UITabBarController 的另一种方法是提供 TabBarInterface

import Foundation
import UIKit

protocol TabBarInterface {
    func configuredViewController() -> UIViewController
}

以便在选项卡栏控制器中呈现视图控制器的每个线框实现 TabBarInterface,然后 installIntoWindow 只是循环遍历所有线框,为它将呈现的每个线框调用 configuredViewController .

import Foundation

import UIKit

class TabBarWireframe : NSObject {

    let wireFrames:[TabBarInterface]
    var rootWireframe : RootWireframe?

    init(_ wireFrames:TabBarInterface...) {
        self.wireFrames = wireFrames
        super.init()
    }

    private override init() {
        self.wireFrames = [TabBarInterface]()
    }

    func installIntoWindow(window: UIWindow) {
        let tabBarController = MainTabBarController()

        var viewControllers = [UIViewController]()

        for wireFrame in wireFrames {
            viewControllers.append(wireFrame.configuredViewController())
        }

        tabBarController.viewControllers = viewControllers
        tabBarController.navigationItem.title = "Visva"

        self.rootWireframe?.installTabBarControllerIntoWindow(tabBarController: tabBarController, window: window)
    }

}

请注意,在我们的例子中 RootWireframe 将标签栏控制器安装到主 Window 中,即:

window.rootViewController = tabBarController
window.makeKeyAndVisible()