如何通过tabBarControllers之间的数据 [英] How to Pass data between tabBarControllers

查看:213
本文介绍了如何通过tabBarControllers之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与一个的UITableViewController 连接到每个之间的两个 UINavigationControllers 传递数据。我通过的UITabBarController 这两个控制器之间进行导航。我一直在试图用在<一个vacawama的解决方案href=\"http://stackoverflow.com/questions/27618737/changing-vc-issue-in-swift-how-to-pass-data-between-views-in-tab-bar-controller\">Changing VC问题斯威夫特。如何通过意见标签栏控制器之间的数据?我使用下面的code。

I am currently trying to pass data between two UINavigationControllers with a UITableViewController attached to each. I am navigating between these two controllers via a UITabBarController. I have been trying to use vacawama's solution on Changing VC issue in Swift. How to pass data between views in tab bar controller? I am using the following code.

import UIKit

class placeData: Equatable {
var description : String
var selected : Bool

init (description : String, selected : Bool) {
    self.description = description
    self.selected = selected
}

}

class PlacesTabBarController: UITabBarController {

var placeDataArray = [placeData]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

这是在tabBarController自定义类。在另外两个控制器我声明我想是视图控制器之间传递和使用的方法中的链接来填充阵列的的UITableViewController

This is in the tabBarController custom class. In the other two controllers I declare the array I want to be pass between the view controllers and use the method in the link to populate the UITableViewController

var placeDataArray = [placeData]()

override func viewWillAppear(animated: Bool) {
    placeDataArray = (self.tabBarController as PlacesTabBarController).placeDataArray
}

当控制器负荷,然而,阵列是空的。在链接的例子中,code的所有剩下的就是 viewWillAppear中功能,我需要我的数组是提供给所有相应的<$内C $ C>的tableView 功能。我不知道如果我只是等同阵列零负载。但我的想法是,他们将重新填充。不知道正确的方式去了解,这是。任何帮助将是巨大的。

When the controllers load, however, the arrays are empty. In the example in the link, all the rest of the code is within the viewWillAppear function, where I need my array to be available to all of the corresponding tableView functions. I am not sure if I am just equating the arrays to zero on load. But my thought was that they would repopulate. Not sure what the correct way to go about this is. Any help would be great.

编辑:我目前的结构如下:

My current structure is as follows:

 UITabBarController
  |              |
UINavigation  UINavigation
Controller     Controller
  |              |
UITableView      UITableView
Controller       Controller

该数组填充时,第一个选项卡的负载。我所试图做的是在第二个视图控制器的阵列人口,如果我在第二个视图控制器编辑它,我要的编辑留在第一。所以,我希望它按引用传递。

The array is populated when the first tab loads. What I am trying to do is have the populated array in the second view controller, and if I edit it in the second view controller, I want the edits to stay in the first. So I want it to be passed by reference.

推荐答案

我就继承了的UITabBarController ,使之成为代表两个 UITableViewControllers

I would subclass the UITabBarController and make it a delegate for the two UITableViewControllers.

protocol CustomTabBarDelegate {
    var places:Array<placeData> { get set }
}

class CustomTabBarController: UITabBarController, CustomTabBarDelegate {
    var places = Array<placeData>()

    override func viewDidLoad() {

        places = [PlaceData(),PlaceData()]

        var table1 = CustomTableViewController()
        var table2 = CustomTableViewController()

        table1.delegate = self
        table2.delegate = self

        var navController1 = UINavigationController(rootViewController: table1)
        var navController2 = UINavigationController(rootViewController: table2)

        self.viewControllers = [navController1, navController2]

    }
}

那么你的 TableViewControllers 只需访问像这样代表数组。

Then your TableViewControllers simply access the delegates array like so.

class CustomTableViewController: UITableViewController {
    var delegate:CustomTabBarDelegate!

    override func viewDidAppear() {
        self.tableView.reloadData()
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return delegate.places.count
    }

}

到阵列的任何更改将可见于每一个表,你 .reloadData()之后 - 我已经设置了 CustomTableViewController 在我的例子显示的每个视图时重新加载数据,因此当您更改标签,他们应该刷新,以显示最新的变化。

Any changes to the array will be visible in each table after you .reloadData() - I have set the CustomTableViewController in my example to reload data every time the view appears, so when you change tabs they should refresh to show the latest changes.

这是值得一提的是,在时间上它很可能是更清洁的有,管理您的数据,而不是捧在TabBarController阵列一个单独的类。

这篇关于如何通过tabBarControllers之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆