首页 > 解决方案 > swift - primaryActionTriggered - 发件人问题

问题描述

我有一个带有可重用单元格的表格视图,每个单元格都有一个文本字段(如篮子中的数量字段)。每次编辑结束或用户点击键盘外的某个地方时,我都需要更新一些数据,所以我同时使用

    @IBAction func quantityEditingDidEnd(_ sender: UITextField) {
            //code
    }
    @IBAction func quantityPrimaryActionTriggered(_ sender: UITextField) {
            let cell = sender.superview?.superview as! BasketTableItemCell
            let indexPath = tableView?.indexPath(for: cell)
            //code
    }

我有一个错误primaryActionTriggered,因为发件人应该是Any,但我需要用来UITextfield检测点击了哪个单元格。我该如何解决这个问题 - 同时使用primaryActionTriggered和检测发件人UIView

*** UPD 我试过用这种方式更改我的代码 -

@IBAction func quantityPrimaryActionTriggered(_ sender: Any) {
print("test")
        if let textField = sender as? UITextField {
            let cell = textField.superview?.superview as! BasketTableItemCell
            let indexPath = tableView?.indexPath(for: cell)
            //code
        }
}

但请注意,控制台上没有线路"test"。所以我想,这@IBAction根本行不通。也许重点在于可重复使用的细胞?didEndEditing效果很好。与 StoryBoard 的连接也可以。

在此处输入图像描述

标签: swiftuitextfieldibaction

解决方案


您可以将元素作为 Any 类型传递,然后像这样进行转换

@IBAction func quantityPrimaryActionTriggered(_ sender: Any) {
        if let textField = sender as? UITextField {
            let cell = textField.superview?.superview as! BasketTableItemCell
            let indexPath = tableView?.indexPath(for: cell)
            //code
        }
}

推荐阅读