将数据从ViewController传递到UITableViewController

时间:2018-10-31 00:33:50

标签: ios swift uitableview

请帮助我了解我所缺少的东西吗?

故事板设置:

ViewController-> TabBarController-> NavigationController,其中包含TableViewController

ViewController TabBarController

都有一个带有标识符“ startUp ”的序列

ViewController 是“初始输入”屏幕。 ViewController 有一个 SegmentedControl 和一个按钮

用法:用户选择其中一个片段并按下按钮。该视图应锁定到TableViewController,并将NavigationItem.Title替换为所选SegmentedControl段中的标题。

问题:数据没有从ViewController传递到TableViewController(以及通过TabBarController和NavigationController)。我总是在TableViewController上以空白标题标题结尾。在过去的几天中,我已经使用segue和其他方法仔细地浏览了数十个“操作方法”,无论是在此处还是在网络上的其他位置。在这一点上,似乎什么都没有起作用,我感到头晕(可能缺少一些非常非常基础的东西)。

这是代码当前“坐下”的方式

ViewController代码:

import UIKit

class ViewController: UIViewController {
    var someData: String? = ""

    @IBOutlet weak var cpChoice: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func startBut(_ sender: Any) {
        performSegue(withIdentifier: "startUp", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let theIndex = cpChoice.selectedSegmentIndex
        someData = cpChoice.titleForSegment(at: theIndex)

        print(someData!) //outputs correctly to console

        if segue.destination is CheckpointVC
        {
            let vc = segue.destination as? CheckpointVC
            vc?.data = someData!
        }
    }
}

TableViewController代码:

import UIKit

class CheckpointVC: UITableViewController {

    var data: String = ""

    @IBOutlet weak var theHeader: UINavigationItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = data
        //theHeader.title = data
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
}

3 个答案:

答案 0 :(得分:0)

您是否在segue上设置了断点以实际检查segue.destination是否为CheckpointVC?解决方案一直就在您的眼前。既然已经这样设置了视图控制器...

ViewController -> TabBarController -> NavigationController which contains a TableViewController

您的segue.destination从技术上讲是TabBarController,除非标识符为startUp的segue导致TableViewVC。

解决此问题的一种简便方法是像这样从TabBarVC中解开TableVC:

(tabBarVC.viewControllers[indexOfYourNavVC] as? NavigationController).viewControllers.first as? TableVC)

更长但更干净的方法是从此博客中实现路由模式。很抱歉,我本人仍在学习有关信息,因此我只能推荐以以下内容开头的网站: https://clean-swift.com/routing-in-clean-swift/

答案 1 :(得分:0)

只需将segue目标替换为包含CheckpointVC作为根视图控制器的导航控制器。

答案 2 :(得分:0)

解决方案

  

零件定义

The pieces

  

ViewController.swift

import UIKit

class ViewController: UIViewController {

    var someData: String? = ""

    @IBOutlet weak var cpChoice: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func startBut(_ sender: Any) {
        performSegue(withIdentifier: "startUp", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        //get the string representing the segment chosen and assign it to someData
        let theSegmentChosen = cpChoice.selectedSegmentIndex
        someData = cpChoice.titleForSegment(at: theSegmentChosen)


        let barVC = segue.destination as! UITabBarController //
        let navigationControllerDestination = barVC.viewControllers?[0] as? navVC //the array index [0] indicates the first view controller belonging to the TabBarController
        let firstViewControllerOfNavigationController = navigationControllerDestination?.topViewController as! CheckpointVC
        firstViewControllerOfNavigationController.data = someData!
    }
}
  

TabBarVC

import UIKit

class TabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

NavVC

import UIKit

class navVC: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

}
  

CheckpointVC.swift

import UIKit

class CheckpointVC: UITableViewController {

    //the variable we'll pass the data to from the starting viewcontroller
    var data: String = ""

    @IBOutlet weak var theHeader: UINavigationItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        theHeader.title = data
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 10
    }
}