gpt4 book ai didi

ios - 如何从 didSelectRowAt 执行 segue

转载 作者:行者123 更新时间:2023-11-28 07:27:05 25 4
gpt4 key购买 nike

我被这个从选定单元格执行 segue 的简单任务困住了。

我创建了一个从一个 ViewController 到另一个具有标识符“showDetails”的 segue。比我尝试这段代码:

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails",
let destination = segue.destination as? DetailsViewController {
let indexPath = tableView.indexPathForSelectedRow!
destination.rideIndex = indexPath.row
}
}

显然它不起作用。我做错了什么?

最佳答案

检查 UITableView 委托(delegate)并确保将 segue 设置为“Present Modally or show”并且:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let destination = segue.destination as! DetailsViewController
let indexPath = tableView.indexPathForSelectedRow!
destination.rideIndex = indexPath.row
}
}

但我个人更喜欢下面的代码来打开 UIViewController:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destination = mainStoryboard.instantiateViewController(withIdentifier: "DetailsViewController") as! DetailsViewController
let indexPath = tableView.indexPathForSelectedRow!
destination.rideIndex = indexPath.row
self.present(destination, animated: true, completion: nil)
}

并确保您的 DetailsViewController 具有标识符“DetailsViewController”

关于ios - 如何从 didSelectRowAt 执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56275727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com