【问题标题】:How to run a segue on itself OR another viewController如何在自身或另一个 viewController 上运行 segue
【发布时间】:2016-05-28 10:26:54
【问题描述】:

我有一个主 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 中设置标识符? 还有另一种方法可以做到这一点吗? 谢谢!

【问题讨论】:

    标签: ios swift uitableview segue


    【解决方案1】:

    目标视图控制器由 segue 设置。由于您有两个不同的目的地,因此您的 Storyboard 中需要两个不同的转场。

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

    然后您可以使用performSegueWithIdentifier 调用它们,就像您在代码中所做的那样。

    【讨论】:

      【解决方案2】:

      不要直接从单元格点击中调用 segue。使用 IBAction 方法(可能在 `didSelectRowAtIndexPath 中,就像您在第二个代码块中显示的那样。

      第二个代码块确实以编程方式设置了 segue 标识符。

      你不能在prepareForSegue中切换segues。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 2015-11-23
        • 1970-01-01
        • 2016-05-07
        • 1970-01-01
        相关资源
        最近更新 更多