ios - 以编程方式展开 segue

标签 ios swift

我正在显示一个带有 UITableView 控件的 UIView。我通过调用 performSegueWithIdentifier 使用 Show segue 显示此 View 。在调用 segue 的模块中,我添加了一个我想在展开时使用的函数,以便在从 segue 返回时从 View 中检索一些数据。现在,我有一个连接到调用方函数的完成按钮,这样我就可以在 segue 展开时从中检索数据。我不想在点击“完成”按钮时执行所有这些操作,而是希望在 UITableView 中选择一个项目时执行此操作。

在 tableView(,willSelectRowAtIndexPath) 中,我尝试调用 popViewControllerAnimated。第二个 UIView 确实关闭并将我带回调用 UIView,但未调用展开函数。

我知道如何使用 Exit 从完成按钮设置展开,但我不知道如何在 UITableView 中点击一行时执行此操作。

这是我用来执行 segue 的代码:

    @IBAction func fromButtonTap(sender: AnyObject) {
    currentButton = sender.tag

    self.performSegueWithIdentifier("UnitsSegue", sender: self)
}

下面是解开 segue 时执行的代码:

@IBAction func returnFromUnitSelection(segue: UIStoryboardSegue) {
    var buttonToSet: UIButton!
    var unitsToSet: UnitData!

    fromLabel.text = "0"
    toLabel.text = "0"

    if let unitsSelectionViewController = segue.sourceViewController as? UnitSelectionController {
        if currentButton == 0 {
            fromUnitData = unitsSelectionViewController.selectedUnit
            buttonToSet = fromButton
        }
        else {
            toUnitData = unitsSelectionViewController.selectedUnit
            buttonToSet = toButton
        }

        setButtonText(buttonToSet, firstLine: unitsSelectionViewController.selectedUnit.unitCode, secondLine: unitsSelectionViewController.selectedUnit.unitName, firstLineFontSize: 15, secondLineFontSize: 11)
    }
}

这是我用来关闭第二个 UIView 的代码:

    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    selectedUnit = temperatures[indexPath.row]
    self.navigationController?.popViewControllerAnimated(true)

    return indexPath
}

当我点击“完成”时,展开代码确实会执行。但是当我在我的表格中选择一个项目时没有任何反应。

最佳答案

您可以为整个 View Controller 创建展开转场。要执行此操作,请将 segue 从 View Controller 对象拖到退出对象。 enter image description here

创建 segue 后,它会列在文档大纲中。

enter image description here

选择它并在属性检查器中给它一个标识符。然后在你的代码中,当 tableview 选择一个单元格时,像任何其他正常的 segue 一样执行这个 segue。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let title = tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text

    performSegueWithIdentifier("backToViewController1", sender: title)
}

然后覆盖 prepareForSegue 方法将数据传递给第一个 View Controller 。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier where identifier == "backToViewController1",
       let destination = segue.destinationViewController as? ViewController,
           selectedTitle = sender as? String {
        destination.selectedCountryTitle = selectedTitle
    }
}

关于ios - 以编程方式展开 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815040/

相关文章:

ios - 删除连字符后的字符串

ios - 为什么(阿拉伯语和英语)文本拒绝正确对齐?

ios - 导航堆栈之前 View 之间的协议(protocol)委托(delegate)

ios - UISwipeGestureRecognizer 未添加到 View 中

ios - 整数文字 '115000159351' 存储到 'Int' 时会溢出,但在一个项目中工作正常,但在另一个项目中则不然 - Swift

ios - 要求不同大小的 San Francisco 字体会在 iOS 13.1 上返回 Times New Roman 变体

ios - swift 不能用 String 调用 isKindOfClass

ios - Swift 编译器错误 - 无法导入桥接头

ios - 找不到图书馆

swift - 什么时候复制 swift 值类型