且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将值传递给另一个 viewController?

更新时间:2022-12-08 16:25:25

你的 prepareForSegue 方法在 didSelectRowAt 方法之前被调用,所以你不能依赖任何逻辑您可以在 didSelectRow 中帮助传递数据.

Your prepareForSegue method is called before the didSelectRowAt method, so you can't rely on any logic you do in didSelectRow to help pass the data.

您可以在 prepareForSegue 中获取所选行并在那里使用它来获取数据并将其传递:

You can get the selected row in prepareForSegue and use it there to get the data and pass it along:

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

    if (segue.identifier == "fromWelcomeToSectionPage") {
         // initialize new view controller and cast it as your view controller
         let vc = segue.destination as! sectionPage
         let selectedRow = tableView.indexPathForSelectedRow!.row
         vc.passedValue = sections[selectedRow]
    }
}