【发布时间】:2015-07-24 19:39:33
【问题描述】:
我正在编写一个基于 TabBar 导航的应用程序。我正在采用 VIPER 架构,但我真的对如何实现 UITabBarController 的选项卡更改的主题感到困惑。
【问题讨论】:
我正在编写一个基于 TabBar 导航的应用程序。我正在采用 VIPER 架构,但我真的对如何实现 UITabBarController 的选项卡更改的主题感到困惑。
【问题讨论】:
这可能会迟到,但可能对其他人有所帮助。 我的用例是在登录屏幕之后实现 tabBarController。 在 VIPER 中有很多方法可以做到,但我的做法如下:
希望我说得通。
【讨论】:
用 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 的新手,所以我的 2 美分可能不值多少钱,但可能会将标签栏作为 AppDelegate 上的私有财产。当您需要更改为特定索引时,可以使用实用方法来更改选项卡栏选定的索引,但也会触发线框/路由器创建过程。
【讨论】: