迅速-primaryActionTriggered-发件人问题

时间:2018-11-22 15:03:32

标签: swift uitextfield ibaction

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

    @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。

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将元素传递为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
        }
}