ios - 点击文本字段时未调用 textFieldDidBeginEditing 函数

标签 ios swift uitextfield

我有一个包含三个 View 的项目,它们都是多个文本字段,在点击时调用 UIPickerView。我有两个 View 工作得很好,但是当我开始第三个 View 时,我无法调用 textFieldDidBeginEditing 函数。我通过在函数的第一行放置一个断点来确认这一点。当我运行时,断点永远不会命中,即使我点击 View 中的任何字段也是如此。我复制并粘贴了其他 View 中的代码,更改了变量名称。

每个文本字段都在 viewDidLoad 方法中初始化为我在之前代码中定义的 dataPickerView 变量。 dataPickerView.delegate = selfdataPickerView.dataSource = self 也是。

这是我认为是相关的代码。我确定我缺少一些简单的东西

@IBOutlet var enterDispPhys: UITextField!
@IBOutlet var enterUser: UITextField!
@IBOutlet var enterInsurance: UITextField!
@IBOutlet var enterBodyPart: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    self.userLabel.text = userName
    self.teamLabel.text = teamName

    //initialize the inputView of the dispensing physician field to the PickerView
    enterDispPhys.inputView = dataPickerView

    //initialize the inputView of the user name field to the PickerView
    enterUser.inputView = dataPickerView

    //initialize the inputView of the insurance field to the PickerView
    enterInsurance.inputView = dataPickerView

    //initialize the inputView of the body part field to the PickerView
    enterBodyPart.inputView = dataPickerView

    dataPickerView.delegate = self
    dataPickerView.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func textFieldDidBeginEditing(textField: UITextField) {
    activeDataArray == [] //clear out the clicked field data array
    if textField == enterDispPhys {
        activeDataArray = dispPhys
    } else
        if textField == enterUser {
            activeDataArray = userNameList
    } else
        if textField == enterInsurance {
            activeDataArray = insCode
    } else
        if textField == enterBodyPart {
            activeDataArray = bodyPart
    }
    dataPickerView.reloadAllComponents()
    dataPickerView.hidden = false
    println(activeDataArray)
}

最佳答案

您是否设置类声明来实现文本字段委托(delegate)?您是否还为每个字段设置了委托(delegate)?我喜欢使用观察者作为电子邮件字段 bellor,但您也可以将其添加到 viewDidLoad 中,就像在密码字段中一样。

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var email: UITextField!{
        didSet{email.delegate = self}
    }
    @IBOutlet weak var password: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        password.delegate = self
    }
}

关于ios - 点击文本字段时未调用 textFieldDidBeginEditing 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30518928/

相关文章:

objective-c - Swift 枚举 Objective-C 中的关联值

arrays - UITextField 没有获取数组的文本值

ios - 修改 iTunesMetadataTrackNumber 音频文件 M4A swift

ios - popViewController 动画 :YES not animating

swift - AVPlayer 无法加载通过 xcode 重新运行应用程序后保存的 .mov 文件

ios - Swift 如何从两个文本字段中获取 Int 并生成随机数

objective-c - iOS:如何将 key 发送到 UITextField?

iOS - 从另一个 ViewController 访问自定义 UITableViewCell 属性。

ios - 读/写数据 NSCoding iOS - Singelton 与否?

ios - 第一部分标题固定在顶部的 UITableView