gpt4 book ai didi

ios - 如何在 Swift 中的两个 UITableViewController 之间传递数据?

转载 作者:行者123 更新时间:2023-11-28 12:51:33 25 4
gpt4 key购买 nike

我一直在尝试使用 Swift 在 XCode 中的两个 UITableView 之间传递参数。

我在这里看到了一些关于 SO 的问题,但即使我按照说明进行操作,我仍然卡住了。

我已经设置了 Storyboard。我在这里遇到了一个问题,因为我找到的大部分指南都使用“显示”Segue,但出于某种原因我只能使用“Push”Segue。我无法拖动并创建“显示”Segue(可能是因为我在 Windows 上通过远程桌面使用 Mac,而 Mac 不理解 Ctrl/Command 键?)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndex = indexPath.row
}

override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
if (segue.identifier == "secondSegue") {
let controller = segue.destinationViewController as! SecondViewController
controller.valuePassed = selectedIndex
}
}

尽管它有效,但出于某种原因,传递的索引永远不是当前索引。相反,第二个屏幕上收到的索引是之前选择的索引。

我也尝试过使用“performSegueWithIdentifier”,但是当我点击一个项目时,我的第二个屏幕打开了两次。我认为这是因为它们之间存在“推送”链接以及“performSegueWithIdentifier”。

我不知道如何在不链接 Storyboard上的两个屏幕的情况下创建 Segue 标识符。

所以,我不确定我的代码是否错误,我是否应该以某种方式创建一个“Show”Segue(我是 iOS 编程的新手,我不知道 Push 和 Show 之间的实际区别UITableView 的上下文),如果我应该在不链接 Storyboard上的屏幕的情况下以某种方式创建 Segue 标识符,等等......我真的被困住了,没有想法。

感谢任何帮助。谢谢。

最佳答案

如果你想使用 performSegueWithIdentifier,请从 View Controller 中拖出 segue,而不是从 TableView 单元格中拖出。否则就像你说的,它会触发两次。

您可以使用 UITableViewindexPathForSelectedRow 属性从 TableView 中获取它,而不是存储 selectedIndex。它返回一个可选的,因此请确保适本地解包它。

关于ios - 如何在 Swift 中的两个 UITableViewController 之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380543/

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