こんにちは私は1つのテーブルビューコントローラから別のものにオブジェクトを渡そうとしています。 私はセグエとdidselectRowAt機能の準備を使用しています:nivのuiviews間でデータを転送
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.myDish = sections[indexPath.section].listofDishes?[indexPath.row]
self.sectionName = sections[indexPath.section].sectionName!
self.flag = 1
performSegue(withIdentifier: "chooseDish", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if self.flag != 0{
let selectedDishTableViewController = segue.destination as! SelectedDishViewController
selectedDishTableViewController.sectionName = self.sectionName
selectedDishTableViewController.dish = self.myDish
self.flag = 0
}
}
prepareforセグエを準備する前に、私はmyDishとセクション名を設定してください作ってるんだdidselectrowat
・タントの道の前に呼び出されるので、私は旗を作成segueのために。
私の2番目のビューコントローラでは、まだ両方の値をnilとして取得します。
これはprepareForSegueが、それはセグエがすでに起こっていることを意味し、... didSelectRow前に呼ばれるので、あなたはそれを強制二度目なっている場合は、私は私の第二のコントローラ
var dish:Dish?
var sectionName:String?
インターフェイスビルダのセルアクションに直接セグを繋ぎ、perfromSegueを使用しようとしているようです。アクションセグを削除します。選択したインデックスパスをあなたのセグに '送信者 'として与えて、' prepare'でアクセスできるようにすることもできます。 – Paulw11