gpt4 book ai didi

ios - 如何在自身或另一个 viewController 上运行 segue

转载 作者:行者123 更新时间:2023-11-29 00:57:22 25 4
gpt4 key购买 nike

我有一个主 TableViewController。当用户点击 TableView 中的单元格时,如果条件语句为真,则再次显示主 TableViewController(但在屏幕上显示另一个数据),如果条件为假,则显示另一个 ViewController。在 prepareForSegue 方法中我有这个:

if  segue.identifier == "identifier1"
{
let destination = segue.destinationViewController as? mainViewController // itself
destination!.x = something // the x variable is in this view controller
}
else if segue.identifier == "identifier2"
{
let destination = segue.destinationViewController as? viewController2
destination!.y = something
}

此外,为了在点击 tableView 中的单元格后运行 segue,我添加了这个:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)

if condition is true {
self.performSegueWithIdentifier("identifier1", sender: self)
}
else{
self.performSegueWithIdentifier("identifier2", sender: self)
}
}

Storyboard segue 需要一个标识符……它只能有一个值!标识符 1 或标识符 2但我想以编程方式设置标识符。我应该如何在 Storyboard segue 中设置标识符?还有另一种方法吗?谢谢!

最佳答案

目标 View Controller 由 segue 设置。由于您有两个不同的目的地,因此您的 Storyboard 中需要两个不同的转场。

诀窍是将 tableViewController 顶部的 viewController 图标的 segues 连接到每个目标 View Controller 。单击生成的 segue 箭头并在 Attributes Inspector 中设置它们的标识符。

wire segue in Storyboard

然后您可以像在代码中那样使用 performSegueWithIdentifier 调用它们。

关于ios - 如何在自身或另一个 viewController 上运行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497870/

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