UITabBarController如何适应VIPER体系结构?
我正在写一个基于TabBar的导航应用程序。 我采用了VIPER体系结构,但是我对如何实现UITabBarController的标签更改这个话题感到困惑。
这可能会很晚,但可能对其他人有帮助。 我的用例是在login屏幕后实现tabBarController。 在VIPER中可以有很多种方法,但是我怎么做呢如下:
- 手动分配TabBar,不使用故事板。
- 仅为TabBarWireframe演示文稿创build一个新的WireFrame类。
- 用一个可变数组创build一个单例类,它将包含所有要分配给tabBarController的视图控制器。
- 创build一个json文件,它将为我提供选项卡的值。这一步可以跳过,因为我希望制表符是基于来自JSON文件的值dynamic生成的。 如果你有静态选项卡,跳过这一步。
- 在TabBarWireframe中,放置一个调用所有标签线框的循环。
- 在你的单独的线框中,实例化viewController obj并将其添加到我们在步骤3中创build的单例类数组中。
- 所有作为选项卡的一部分的viewController都是数组的一部分。 只要从loginviewcontroller实例(它的实例只是通过一个方法到tabBarWireframe类)呈现tabBar控制器。
希望我有道理。
用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.rootViewController = tabBarController window.makeKeyAndVisible()
对于VIPER,我还是个新手,所以我的两个分钱可能不值多less,但也许有一个Tabbar作为AppDelegate的私有属性。 当您需要更改为特定索引时,可以使用实用程序方法来更改选项卡栏选定的索引,但也会触发线框/路由器创build过程。