不能通过segue将数据传递到第二个控制器

时间:2018-11-26 14:16:07

标签: ios swift segue

当按下表视图中的单元格时,我无法进行简单的设置。在点击了两个不同的项目后,它确实转到了下一个视图。但是我无法将任何值从第一个控制器传递给第二个。如果我在第二个控制器中为标签设置一个值并以viewDidLoad方法加载它,则会显示出来。

我已经疯了很久了,因为我一直在努力进行这项工作。

我的故事板:https://snag.gy/DCw9MU.jpg

CategoryListViewController(第一个控制器):

import Foundation
import UIKit

class CategoryListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var categoryList = TestData.sharedInstance.categoryList


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "iEngineer"
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.tableView .reloadData()
        tableView.dataSource = self
        for category in categoryList{
            print(category)
        }

    }

    // MARK: - Segues
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showFormulaList" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let category = self.categoryList[indexPath.row]
                let formulaListViewController = (segue.destination as! UINavigationController).topViewController as! FormulaListViewController
                formulaListViewController.text = category
                formulaListViewController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                formulaListViewController.navigationItem.leftItemsSupplementBackButton = true
            }
        }

    }

    // MARK: - Table View
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(categoryList.count)
        return categoryList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath)
        let object = categoryList[indexPath.row]
        cell.textLabel!.text = object
        return cell
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showFormulaList", sender: self)
    }
}

FormulaListViewController(第二个控制器):

import Foundation
import UIKit

class FormulaListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var titleLabel: UILabel!

    var formulaList = TestData.sharedInstance.formulaList

    var fSwift: String!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "iEngineer"
        print(fSwift)
        titleLabel.text = fSwift

    }

    // MARK: - Table View
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(formulaList.count)
        return formulaList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "formulaCell", for: indexPath)
        let object = formulaList[indexPath.row]
        print(object)
        cell.textLabel!.text = object
        return cell
    }
}

我的错误在哪里或我在做什么错?

非常感谢您的帮助

3 个答案:

答案 0 :(得分:2)

您需要didSelectRowAt而不是didDeselectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showFormulaList", sender: self)
}

还要确保segue的源是vc而不是单元,并且由于您在didDeselectRowAt中触发了segue

if let indexPath = tableView.indexPathForSelectedRow 

将为

答案 1 :(得分:0)

您已使用didDeselectRowAt

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showFormulaList", sender: self)
}

您需要使用didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "showFormulaList", sender: self)
}

答案 2 :(得分:0)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showFormulaList", sender: self)
}

您应该使用didSelect而不是didDeselectRowAt,还应该传递比 self 更好的东西,因为使用self,您将传递整个 CategoryListViewController 尝试像这样传递indexPath

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showFormulaList", sender: indexPath)
}

并更改在此准备的功能

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showFormulaList" {
        if let indexPath = sender as? IndexPath {
            let category = self.categoryList[indexPath.row]
            let formulaListViewController = (segue.destination as! UINavigationController).topViewController as! FormulaListViewController
            formulaListViewController.fuckSwift = category
            formulaListViewController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
            formulaListViewController.navigationItem.leftItemsSupplementBackButton = true
        }
    }

}

如果这不能帮助您尝试调试代码并查找丢失变量的位置:)