Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将协调器与UIITabBarController一起使用?_Ios_Swift_Mvvm_Uitabbarcontroller_Protocols - Fatal编程技术网

Ios 如何将协调器与UIITabBarController一起使用?

Ios 如何将协调器与UIITabBarController一起使用?,ios,swift,mvvm,uitabbarcontroller,protocols,Ios,Swift,Mvvm,Uitabbarcontroller,Protocols,我正在使用MVVM-C体系结构,但我不确定在选择一个选项卡时如何使用不同的选项卡实例化多个协调员 这是我的主要应用程序协调员课程 protocol UINavigationControllerType: class { func pushViewController(_ viewController: UIViewController, animated: Bool) func popViewController(animated: Bool) -> UIViewController? }

我正在使用MVVM-C体系结构,但我不确定在选择一个选项卡时如何使用不同的选项卡实例化多个协调员

这是我的主要应用程序协调员课程

protocol UINavigationControllerType: class {
func pushViewController(_ viewController: UIViewController, animated: Bool)
func popViewController(animated: Bool) -> UIViewController?
}

protocol Coordinator: class {
func start()
}

final class AppCoordinator: Coordinator {
// MARK: - Properties
var managedObjectContext: NSManagedObjectContext!
var coordinators = [String : Coordinator]()

var tabController: UITabBarController?

// MARK: - Object Lifecycle
init(moc: NSManagedObjectContext, tabController: UITabBarController) {
    self.managedObjectContext = moc
    self.tabController = tabController
}

// MARK: - Coordinator
func start() {
    guard let tabController = tabController else {return}

    let profileNavigationController = NavigationController()
    profileNavigationController.tabBarItem = UITabBarItem(title: "Profile", image: UIImage(named: "profileUnselected"), selectedImage: UIImage(named: "profileSelected"))

    let plansNavigationController = NavigationController()
    plansNavigationController.tabBarItem = UITabBarItem(title: "Plans", image: UIImage(named: "plansUnselected"), selectedImage: UIImage(named: "plansSelected"))

    tabController.viewControllers = [profileNavigationController, plansNavigationController]
    tabController.selectedViewController = profileNavigationController

    let profileCoordinator = ProfileCoordinator(navigationController: profileNavigationController)
    profileCoordinator.managedObjectContext = managedObjectContext
    coordinators["profileCoordinator"] = profileCoordinator
    profileCoordinator.delegate = self
    profileCoordinator.start()
}
}

// MARK: - ProfileCoordinatorDelegate
extension AppCoordinator: ProfileCoordinatorDelegate {}

那么,当选择选项卡时,我将如何从当前协调器(ProfileCoordinator)转到PlansCoordinator?

我的协调器结构与您的不同,但它可能会帮助您。在我的例子中,协调器协议有一个
rootViewController
属性,它指向该协调器的ViewController

My
AppCoordinator
然后会持续出现一个
TabCoordinator
,看起来有点像这样:(在实际代码中,持久化协调员是
navigationconcerators
,它们是专门的协调员,它包含导航控制器。在本例中,我只添加了ViewController并删除了内存管理内容,以便于理解。)

因此,基本上,您的TabBarController是一个TabCoordinator,它的
rootViewController
是一个TabBarController。TabCoordinator实例化相关的协调员,并将其各自的
rootViewController
添加到选项卡中

下面是
导航协调器的基本实现:

class NavigationCoordinator: NSObject, Coordinator {    

    public var navigationController: UINavigationController     

    public override init() {
        self.navigationController = UINavigationController()
        self.navigationController.view.backgroundColor = .white
        super.init()
    }    

    public var rootViewController: UIViewController {
        return navigationController
    }
}
public protocol Coordinator: class {    
    var rootViewController: UIViewController { get }    
}
以及协调器的基本版本:

class NavigationCoordinator: NSObject, Coordinator {    

    public var navigationController: UINavigationController     

    public override init() {
        self.navigationController = UINavigationController()
        self.navigationController.view.backgroundColor = .white
        super.init()
    }    

    public var rootViewController: UIViewController {
        return navigationController
    }
}
public protocol Coordinator: class {    
    var rootViewController: UIViewController { get }    
}

我想分享一个关于这个问题的例子。我的方法有点不同,并且
TabCoordinator
不包含他身体中的所有内容。相反,每个协调员都与其控制器和每个
UIViewController
(在tabbar控制器中)有关系具有其
UITabBarController
引用式委托模式

class Coordinator {

    var navigationController: UINavigationController?
    var childCoordinators: [Coordinator] = [Coordinator]()

    init(with navigation: UINavigationController) {
        self.navigationController = navigation
    }

    func start() {}

}
然后是协调员

家庭协调员

关于协调员

超宽带控制器

从AppDelegate准备应用程序

在应用程序中(u应用程序:UIApplication,didFinishLaunchingWithOptions函数

这是AboutController

维护协调员

final class maintab协调器:协调器{
var currentController:TabController?
覆盖初始化(带导航:UINavigationController){
super.init(带:导航)
self.currentController=TabController()
self.childCoordinators.append(self)
currentController?.coordinator=self
}
覆盖函数开始(){
navigationController?.pushViewController(currentController??UIViewController(),
动画:真)
}
func fetchHome(标题:String,完成:@escaping(uu结果:Dictionary,uu错误:NSError?)->()){
完成([“ss”:“ss”],无)
}
func handleAbout(标题:String,完成:@escaping(uu结果:Dictionary,u错误:NSError?)->()){
完成([“ss”:“ss”],无)
}
}
工作模式如下所示:


AboutController
-->触发的操作--->
AboutCoordinator
-->
tabbar控制器参考
请求关于操作--->
MainTabCoordinator
处理工作。

我可以看一个导航协调员的例子吗?很抱歉回复太晚。喂,布鲁诺,如果你还在读这篇文章的话。我能看看你的解决方案吗?你是如何处理内存管理的?谢谢你发布这篇文章,这是我需要的一个很好的起点。你能分享你的一个协调员代码吗,例如:HomeCoordinator?HTTP?它在选项卡栏控制器代码中做什么?你能添加演示链接吗。
final class AboutCoordinator: Coordinator {

    var currentController: AboutController?
    weak var tabController: TabController?

    override init(with navigation: UINavigationController) {
        super.init(with: navigation)

        currentController = AboutController()
        currentController?.coordinator = self
        childCoordinators.append(self)
    }


    override func start() {

        navigationController?.pushViewController(currentController ?? UIViewController(),
                                                 animated: true)

    }

    public func getAboutData() {

        // GETTING ABOUT DATA

        tabController?.requestFromAboutController()

    }
}
final class TabController: UITabBarController {

    weak var coordinator: MainTabCoordinator?


    override func viewDidLoad() {
        super.viewDidLoad()


        let navigationController1 = UINavigationController()
        let coordinator1 = HomeCoordinator(with: navigationController1)
        coordinator1.tabController = self
        coordinator1.currentController?.tabBarItem = UITabBarItem(title: "HOME",
                                                                  image: nil,
                                                                  tag: 11)

        let navigationController2 = UINavigationController()
        let coordinator2 = AboutCoordinator(with: navigationController2)
        coordinator2.tabController = self
        coordinator2.currentController?.tabBarItem = UITabBarItem(title: "ABOUT",
                                                                  image: nil,
                                                                  tag: 22)


        viewControllers = [
            coordinator1.currentController!,
            coordinator2.currentController!
        ]

        tabBar.barTintColor = UIColor.white
        tabBar.isTranslucent = false

    }

    public func requestFromHomeController() {
        print("Home Triggered the function")

        coordinator?.fetchHome(with: "Simple Data",
                               completion: { (dictionary, error) in
                                print("dict from home -> ", dictionary)
        })



    }

    public func requestFromAboutController() {
        print("About Triggered the function")

        coordinator?.handleAbout(with: "Simple Data",
                                 completion: { (dictionary, error) in
                                    print("dict from about -> ", dictionary)
        })
    }    
}
let appNavigationController = UINavigationController()
let tabCoordinator = MainTabCoordinator(with: appNavigationController ?? UINavigationController())
tabCoordinator.start()
window?.rootViewController = appNavigationController
final class AboutController: UIViewController{

    weak var coordinator: AboutCoordinator?

    // define a button and add its target to handleButton function

    @objc private func handleButton(_ sender: UIButton) {

        coordinator?.getAboutData()
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // ui settings
    }
}
final class MainTabCoordinator: Coordinator {

    var currentController: TabController?


    override init(with navigation: UINavigationController) {
        super.init(with: navigation)

        self.currentController = TabController()
        self.childCoordinators.append(self)
        currentController?.coordinator = self
    }



    override func start() {

        navigationController?.pushViewController(currentController ?? UIViewController(),
                                                 animated: true)

    }

    func fetchHome(with title: String, completion:  @escaping (_ result: Dictionary<String,Any>, _ error: NSError?) -> ()) {

        completion(["ss":"ss"], nil)

    }


    func handleAbout(with title: String, completion: @escaping (_ result: Dictionary<String,Any>, _ error: NSError?) -> ()) {

        completion(["ss":"ss"], nil)
    }

}