ios - 如何以编程方式创建可编辑的 UITextField

标签 ios iphone swift uitextfield uitextfielddelegate

我正在使用 Xcode 7.3.1 在 Swift 中编写一个应用程序以在 iOS 9.3 上运行。我正在以编程方式创建 UITextFields,但当我单击其中之一时无法让它们接受焦点。以下是我创建每个 UITextField 的方法:

self.hoursField = UITextField()
self.hoursField!.borderStyle = UITextBorderStyle.Bezel
self.hoursField!.canBecomeFirstResponder()
self.hoursField!.canBecomeFocused()
self.hoursField!.delegate = self
self.hoursField!.enablesReturnKeyAutomatically = true
self.hoursField!.translatesAutoresizingMaskIntoConstraints = false
self.hoursField!.userInteractionEnabled = true

UITextFields 完全出现在我想要的位置,并且看起来完全按照我想要的样子,我只是无法让它们接受焦点,因此我无法编辑字段中的文本。

我正在实现 UITextFieldDelegate 协议(protocol)中的所有方法,并从所有返回 bool 值的方法中返回 true。

谁能解释一下我做错了什么,或者没做什么?

每个文本字段都被创建并添加到 UIView 的自定义子类中,除了 UITextField 之外,该子类还包含几个 UILabel。以下是创建文本字段的自定义 View init 方法的相关部分:

init(frame: CGRect, charge: (code:String, note:String?, hours:Int)) {
    super.init(frame: frame)

    // Create the hours UITextField
    self.hoursField = UITextField()
    self.hoursField!.borderStyle = UITextBorderStyle.Bezel
    self.hoursField!.canBecomeFirstResponder()
    self.hoursField!.canBecomeFocused()
    self.hoursField!.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
    self.hoursField!.delegate = self
    self.hoursField!.enablesReturnKeyAutomatically = true
    self.hoursField!.font = UIFont.systemFontOfSize(14)
    self.hoursField!.keyboardType = UIKeyboardType.NumberPad
    self.hoursField!.returnKeyType = UIReturnKeyType.Done
    self.hoursField!.text = String(charge.hours)
    self.hoursField!.textAlignment = NSTextAlignment.Right
    self.hoursField!.translatesAutoresizingMaskIntoConstraints = false
    self.hoursField!.userInteractionEnabled = true

    // Add it to the view
    self.addSubview(self.hoursField!)

    // Create its layout constraints
    let hoursTopMarginConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.hoursField!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.codeLabel!, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
    let hoursLeftMarginConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.hoursField!, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.codeLabel!, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: self.hourFieldIndent)
    let hoursRightMarginConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.hoursField!, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.hoursField!, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: self.hourFieldWidth)

    // Add the layout constraints to the view
    self.addConstraints([hoursTopMarginConstraint, hoursLeftMarginConstraint, hoursRightMarginConstraint])

我的应用程序的精简版本显示了我遇到的问题,可以从 https://github.com/ThomBrando/TextFieldDemo.git 下载

最佳答案

你试过吗

self.hoursField!.becomeFirstResponder() 

关于ios - 如何以编程方式创建可编辑的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195936/

相关文章:

iphone - 我怎么知道这个实例在哪里被释放?

iphone - 计算 imagepickerdidFinishPickingMediaWithInfo 中的视频大小(以字节为单位)

swift - 联系人和类别位掩码 - 只需要设置其中之一还是两者都需要设置?

ios - 如何使用自动布局垂直堆叠 TableView ?

ios - 如何在 swift 中将 View 设置为 UIViewController 的最大边界?

iphone - 当一个 subview 大小发生变化时,如何调整其他 subview 和父 View 的大小

ios - Swift 中的字典,将枚举键映射到函数调用

xcode - 您可以使用 HLS 从 HTTPS 服务器流式传输吗?

iphone - iOS 企业应用 : How to roll out updates?

ios - Google didSignInForUser 不会在 swift 3 中触发