在视图控制器之间传递数据出错

时间:2017-05-05 23:44:29

标签: ios swift segue

我试图在Swift中将数据从一个视图控制器传递到另一个视图控制器。

查看控制器1:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let selectedFiName = self.fetchedFiName[indexPath.row].fiName
    let selectedOneYear = self.fetchedFiName[indexPath.row].oneYear
    let selectedTwoYear = self.fetchedFiName[indexPath.row].twoYear
    let selectedThreeYear = self.fetchedFiName[indexPath.row].threeYear
    let selectedFourYear = self.fetchedFiName[indexPath.row].fourYear
    let selectedFiveYear = self.fetchedFiName[indexPath.row].fiveYear

    passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]


    performSegue(withIdentifier: "SecondViewController", sender: self)
}

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

    if let selectedFiName = sender as!  ,
    let destVC = segue.destination as? SecondViewController {
        destVC.fiName = selectedFiName
    }
}

错误显示在"准备segue"在视图控制器2中(接收):

struct SecondTable {

    var passedFIName: String = ""
    var passedOneYear: String = ""
    var passedTwoYear: String = ""
    var passedThreeYear: String = ""
    var passedFourYear: String = ""
    var passedFiveYear: String = ""

}

class SecondViewController: UIViewController {

    @IBOutlet weak var fiName: UILabel!
    @IBOutlet weak var sometext: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let fiDetails = SecondTable()

        fiName.text = SecondTable.passedFIName
        sometext.text = "Some Text"

3 个答案:

答案 0 :(得分:0)

这一位:

if let selectedFiName = sender as!  ,

甚至不应该编译。

你需要一个类型,比如

if let selectedFiName = sender as? String,
    let destVC = segue.destination as? SecondViewController {
  //Your code here
}

答案 1 :(得分:0)

视图1:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     // create your struct object but in this example you're not passing an array so get rid of the [ ]

     presentNextView(passing: passData)
}

func presentNextView(passing: SecondTable) {
     let next = self.storyboard?.instantiateViewController(withIdentifier: "identifierHere") as! View2
     next.fiDetails = passing
     self.present(next, animated: true) 
}

identifierHere在view.Conorylers类的View2(您要呈现的那个)属性检查器的main.storyboard中设置。为简单起见,我通常将其与ViewController名称保持一致,但字符串必须完全匹配,否则您将崩溃。

在View2中,您只需要声明您的变量:

var fiDetails = SecondTable()

在视图控制器类的开头执行,而不是在viewDidLoad

我知道这不是一个segue但是我发现它更方便,因为你也不需要在你的故事板中建立任何连接,下一个视图从函数而不是segue中呈现。

答案 2 :(得分:0)

如果要将PassData结构传递给第二个视图控制器,请将其作为sender中的performSegue...参数传递

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let fetchedItem = self.fetchedFiName[indexPath.row]
    let selectedFiName = fetchedItem.fiName
    let selectedOneYear = fetchedItem.oneYear
    let selectedTwoYear = fetchedItem.twoYear
    let selectedThreeYear = fetchedItem.threeYear
    let selectedFourYear = fetchedItem.fourYear
    let selectedFiveYear = fetchedItem.fiveYear

    passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]

    performSegue(withIdentifier: "SecondViewController", sender: passData)
}

在目标视图控制器中,创建属性fiDetails而不是局部变量。然后将文本分配给标签的text属性。

class SecondViewController: UIViewController {

    @IBOutlet weak var fiName: UILabel!
    @IBOutlet weak var sometext: UILabel!

    var fiDetails = SecondTable()

    override func viewDidLoad() {
        super.viewDidLoad()

        fiName.text = fiDetails.passedFIName
        sometext.text = "Some Text"

prepare(for中的源视图控制器中,从sender参数获取结构并将其分配给fiDetails属性:

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

    if segue.identifier == "SecondViewController" {        
        let destVC = segue.destination as! SecondViewController
        let fiDetails = sender as! SecondTable
        destVC.fiDetails = fiDetails
    }
}