【问题标题】:Unable to share data between tab view controllers?无法在选项卡视图控制器之间共享数据?
【发布时间】:2020-03-13 15:05:51
【问题描述】:

我正在制作一个应用程序,我在其中接收用户输入并将其显示为图表,这需要一组数据。我已经设法使用核心数据将数据保存在数组中,但我无法弄清楚如何将数据从一个选项卡共享到另一个 TabViewController。

这是数据在 FirstViewController 中的存储和获取方式

        let number = Numbers(context: PersistenceService.context)
        number.numberInArray = Int16(numberEnteredInSlider)
        PersistenceService.saveContext()
        testArray.append(Int(Double(number.numberInArray)))

            var numbers  = [Numbers]() // Where Numbers =  NSManaged Class
            var fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Numbers")
            do {try  numbers = PersistenceService.context.fetch(fetchRequest) as! [Numbers]
            for number in numbers {
                print(number.numberInArray)
            }
            }catch {
                print("error")
            }

这是输出(打印的测试数组):

SAVED
2
5
6
5

现在我想将这个测试数组从一个视图控制器共享到另一个(chartsViewController) 这是我尝试过的

class chartsViewController: UIViewController {
    let mainVC = mainViewController(nibName: "mainViewController", bundle: nil)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(mainVC.testArray)
        updateGraph()

    func updateGraph() {
        var lineChartEntry  = [ChartDataEntry]() //this is the Array that will eventually be displayed on the graph.

        for i in 0..<mainVC.testArray.count {
            //

            let value = ChartDataEntry(x: Double(i), y: Double(mainVC.testArray[i]))
            // here we set the X and Y status in a data chart entry

            lineChartEntry.append(value)
            // here we add it to the data set
        }}
//only showing the part needed. I have tried the same solution with another array and it worked. 

}

输出为[0] 我也尝试过制作单身人士,但没有成功。

【问题讨论】:

  • 所以单例行不通。在进入segue之前将它注入chartsViewController怎么样?
  • 如果我错了,我很抱歉,但我不会在选项卡视图控制器中从一个选项卡转到另一个选项卡。如果是,我不知道使用的是哪个函数。
  • 您不需要在另一个视图控制器中使用来自FirstViewControllertestArray。如果您已经使用 Core Data 保存数据,只需将此数据提取到 chartsViewController 中的新数组中。
  • @AlexSmet,我做到了,它成功了,谢谢!

标签: swift ios-charts


【解决方案1】:

要在 UITabBarController /tabBar 上的选项卡之间传递数据,需要做的是有一个中间件。 (这通常是主UITabBarController)

Pic of UITabBarController and the child tabbar

创建一个类并将其链接到 IB 中的这个 TabBarController

class BaseTBController: UITabBarController {

  // Provide the variable which we want to pass
  var workoutTitle: String = "Select a Workout"
  
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

假设您想将数据从 TabBar2 传递到 TabBar1,然后在 TabBar2 上(在本例中,我将其作为 UITableView)。在委托方法中:

extension VCLibrary: UITableViewDelegate{
  // method to run when table view cell is tapped
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    // prepare to store the data to be passed to another TabBar
    let tabbar = tabBarController as! BaseTBController
    tabbar.workoutTitle = jsonErgWorkouts[indexPath.row].title

    // Automatically select Tab1 after choosing
    self.tabBarController?.selectedIndex = 0

    // Deselect the selected row once we move to Tab1
    tableView.deselectRow(at: indexPath, animated: true)
  }
}

选择要传递的数据后,代码(上)会自动切换到Tab1。在 Tab1 中,以下代码旨在接收传递的数据

override func viewDidAppear(_ animated: Bool) {
    // Obtain Passed in values from BaseTBController
    let tabbar = tabBarController as! BaseTBController

    // populate the Title as passed from Tab2
    workoutTitleLabel.text = tabbar.workoutTitle
}

我从以下方面了解到这一点: https://www.youtube.com/watch?v=GL8-eM93EvQ

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多