ios - Swift 准备 Segue : How to know If a button or a table row initiated Segue

标签 ios swift segue

这是我的代码。我正在尝试执行segue。我有一个栏按钮项目以及一个表格,其中的行执行相同的序列。我想知道何时单击按钮以及何时单击特定行。

以下代码适用于按钮,但不适用于表格的行

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    activePlaceRow = indexPath.row

    performSegue(withIdentifier: "toMap", sender: nil)

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "toMap"
    {
        let mapViewController = segue.destination as! ViewController

        if let senderObject = sender
        {
            if (senderObject as! UIBarButtonItem).tag == 1
            {

                mapViewController.activePlaceRow = -2
            }
            else
            {
                mapViewController.activePlaceRow = activePlaceRow
            }
        }

    }
}

最佳答案

只需将索引路径作为发送者传递

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: "toMap", sender: indexPath)

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "toMap"
    {
        let mapViewController = segue.destination as! ViewController

        if let buttonItem = sender as? UIBarButtonItem, buttomItem.tag == 1 {
            mapViewController.activePlaceRow = -2
        }
        else if let indexPath = sender as? IndexPath {
            mapViewController.activePlaceRow = indexPath.row
        }
    }
}

关于ios - Swift 准备 Segue : How to know If a button or a table row initiated Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44726988/

相关文章:

ios - 从 cocos2d 1.x 升级到 2.x 后帧率下降

uitableview - ios uitableviewcontroller编辑模式

ios - 无法将 UIScrollView 设置为在 UITableViewCell 中滚动

ios - 通过导航 Controller 通过 segue 传递数据 - Objective C

ios - 登录 View /记录 View 和 segues : what is the correct workflow to adopt?

ios - 使用属性文本在 UITextView 中重绘 NSTextAttachments

ios - Swift & SpriteKit - 如何在 GameScene 中呈现警报 View

swift - 将两个泛型类型作为函数参数传递

swift - 如何使用子类来遵守协议(protocol)

ios - 如何在 Swift 中关闭当前的 ViewController 并转到另一个 View