ios - 将 UITextfield 从不可编辑更改为可编辑 - Swift

标签 ios swift uitextfield

我正在尝试让两个不可编辑的 UITextField 显示 nameage。我的导航栏中有一个 Edit UIBarButtonItem,我希望能够在按下该按钮时触发 UITextField 以进行编辑。

在我的 Interface Builder 中,我为两个 UITextField 取消选中了 User Interaction Enabled 选项。我是否需要添加 ageText.UserInteractionEnabled = true?我在这里不知所措。

class UserProfileVC: UIViewController, 
UITextFieldDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var infoBorder: UILabel!
    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var ageText: UITextField!

    var textFields:[UITextField] = []

    @IBAction func editButton(sender: UIBarButtonItem) {
        nameText.becomeFirstResponder()
        ageText.becomeFirstResponder()
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        var currentTextField = textFields[0]

        if (currentTextField == textField) {
            currentTextField = textFields[1]
            currentTextField.becomeFirstResponder()
        } else {
            currentTextField.resignFirstResponder()
        }

        return true
    }
}

最佳答案

是的,你想做

nameText.userInteractionEnabled = true
ageText.userInteractionEnabled = true

可能

nameText.becomeFirstResponder()

第一次按下编辑按钮时。您可能还想将“编辑”按钮更改为“完成”按钮。当用户按下完成按钮时,您需要确保禁用用户交互并退出两个文本字段的第一响应者。

关于ios - 将 UITextfield 从不可编辑更改为可编辑 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059994/

相关文章:

ios - iOS Facebook SDK v4.5.0。如何与帖子分享短信?

ios - 如果字符串为空则抛出异常

swift - 如何找到在 HitTest 中找到的平面中心 ARKit SceneKit

ios - 使用 CloudKit 进行轻量级 CoreData 迁移

ios - 自动调整大小时 UITextField 字体粗细发生变化

ios - shouldChangeCharactersInRange 没有被调用

ios - NSLog nsarray 中的 nsarray

ios - 使用委托(delegate)时弹出 ViewController

ios - 如果禁用 UITextField(在相应的 UIView 中),更改 UILabel 的颜色? ( swift )

ios - 当 VoiceOver 处于事件状态时,关闭 UITextField 中的 UIKeyboardTypeNumberPad?