【问题标题】:Swift UI Delegate methods not being workingSwift UI 委托方法不起作用
【发布时间】:2019-11-22 05:28:48
【问题描述】:

我试图让我的 UITextField 中的输入显示在调试器控制台中,当我在创建的 TextField 中键入时,委托方法似乎没有响应。我希望看到我的 UIdelegate 方法在下面看到的打印语句,例如当我第一次开始输入时、输入时以及按下“返回键”时。所有委托方法似乎都没有被激活,我不知道如何让我的 Textfield 直接链接到委托方法。此外,我还有另一个 UITextField(此处未显示),我是否必须通过“addTarget”来区分两者?

class ViewController: UIViewController, UITextFieldDelegate {    

 let createUserName: UITextField = {
    var myTextField = UITextField ()
    myTextField.translatesAutoresizingMaskIntoConstraints = false
    myTextField.placeholder = "Username" //set placeholder text
    myTextField.font = UIFont.systemFont(ofSize: 14) // set font size of text field
    myTextField.layer.borderWidth = 1.0 //set width
    myTextField.layer.borderColor = UIColor.red.cgColor//set background color to a ui color
    myTextField.layer.backgroundColor = UIColor.white.cgColor
    myTextField.layer.cornerRadius = myTextField.frame.height/2
    myTextField.autocorrectionType = .no // disable autocorrect when typing for .no, enable with .yes
    myTextField.isSecureTextEntry = false// masked text
    myTextField.keyboardType = .default //keyboard style is set to default
    myTextField.returnKeyType = .default //retuen key text changed to "Done" instead of return
    myTextField.clearButtonMode = .whileEditing
    myTextField.delegate = self as? UITextFieldDelegate
    return myTextField
}()

 override func viewDidLoad() {
    super.viewDidLoad()

 createUserName.delegate = self
    view.addSubview(createUserName)
    setupUserName()
}

//UITextField Delegate methods

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    print("textfield should begin editting")
    return true
}

func textFieldDidBeginEditing(_ textField: UITextField) {
print("text field edit")
}


//see string that is typed in debugger for use to validate password and crossreference username
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

if let textFieldString = textField.text, let swtRange = Range(range, in: textFieldString) {

let fullString = textFieldString.replacingCharacters(in: swtRange, with: string)

print("FullString: \(fullString)")
}

return true
}

//dismiss keyboard when return button is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    print("text field return")
    return true
}

}

【问题讨论】:

    标签: swift debugging delegates uitextfield uitextfielddelegate


    【解决方案1】:

    你的 viewController 应该继承自 UITextFieldDelegate

    class YourViewController : UIViewController, UITextFieldDelegate { 
     // your code
    } 
    

    同样在您的 ViewDidLoad 中,将您的 createUsername.delegate = self 移动到最后一行。

    【讨论】:

    • @keshurrai 我在上面的问题陈述的类行中添加了这样您就可以看到所有内容。将 createUsername.delegate = self 放在 ViewDidLoad 的最后一行不起作用。还有其他建议吗?
    • @keshurrai 我意识到我的 } 闭包之一不包含我的类中的委托方法,所以现在它可以工作了
    【解决方案2】:

    那个字符串:

    myTextField.delegate = self as? UITextFieldDelegate
    

    告诉我们你的 VC 不直接符合协议 UITextFieldDelegate...

    如果你符合 swift 不添加 as?投...

    【讨论】:

    • 你是说拿 myTextField.delegate = self 作为? UITextFieldDelegate out?
    • 我意识到我的 } 闭包之一不包含我的类中的委托方法,所以现在它可以工作了
    • 恭喜! @贝恩
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多