【问题标题】:How to check which textfield is pressed?如何检查按下了哪个文本字段?
【发布时间】:2017-11-13 21:25:13
【问题描述】:

我想根据已单击的textfield 更改数组的值。我有以下代码:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        switch textField.tag {
        case 1:
            currentHospitals = hospitalsMCT
        case 2:
            currentHospitals = hospitalsSohar
        case 3:
            currentHospitals = hospitalsMusandam
        default:
            return false
        }
        return false
    }

但是当我按下 alt 并将鼠标悬停在 textFieldShouldBeginEditing 上时,它无法识别该功能。

我在这里做错了什么?

还有更好的方法来检测哪个textfield被按下了吗?

【问题讨论】:

  • func textFieldDidBeginEditing(_ textField: UITextField) 这将首先调用
  • @UmaMadhavi 谢谢,现在试试
  • @UmaMadhavi 我需要继承什么类吗?因为我也不能覆盖那个方法
  • @BehrouzRiahi,你委托文本字段了吗?类 ViewController: UIViewController, UITextFieldDelegate { } textfield.delegate = self;
  • @UmaMadhavi 没关系,这是新手问题,我必须继承 UITextFieldDelegate

标签: ios swift swift3 uitextfield


【解决方案1】:

只需在视图控制器中键入该代码

// MARK: - 变量 var txtTemp: UITextField!

// MARK: - 委托方法 // MARK: UITextField

func textFieldDidBeginEditing(_ textField: UITextField) {
    txtTemp = textField
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多