【问题标题】:swift - primaryActionTriggered - sender problemswift - primaryActionTriggered - 发件人问题
【发布时间】:2018-11-22 15:03:31
【问题描述】:

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

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

【问题讨论】:

    标签: swift uitextfield ibaction


    【解决方案1】:

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

    【讨论】:

    • 对不起!没看到啊!您将什么 textField 的操作链接到 quantityPrimaryActionTriggered ?
    • 添加了 xcode 的截图。 primaryAction 和 editingDidEnd。
    • 好吧,也许你可以使用另一个动作来做你想做的事,这取决于你从文本字段中等待什么来触发该功能;也许你想调用editingDidBegin。您也可以在代码中使用委托函数,也许它可以提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2011-01-30
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多