gpt4 book ai didi

swift - 读取 UITextfields 的值时出错

转载 作者:搜寻专家 更新时间:2023-11-01 06:29:25 24 4
gpt4 key购买 nike

我在读取文本字段中的值时遇到问题。每次我在这一行中输入 let username = UsernameTextField.text 时,我都会收到一条错误消息“无法在属性初始值设定项中使用实例成员‘UsernameTextField’;属性初始值设定项在‘self’可用之前运行”。

完整代码如下:

import UIKit

class SignInViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!
@IBOutlet var LogInButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
navigationController?.view.backgroundColor = UIColor.clear
// Remove Autocorrection Type
UsernameTextField.autocorrectionType = .no
PasswordTextField.autocorrectionType = .no
PasswordTextField.textContentType = UITextContentType("")
//Next button takes user to the next textfield
UsernameTextField.delegate = self
PasswordTextField.delegate = self
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UsernameTextField{
PasswordTextField.becomeFirstResponder()

} else if textField == PasswordTextField {
self.view.endEditing(true)
}
return true
}

let username = UsernameTextField.text
}

最佳答案

这就是您所拥有的,设置文本的调用不在函数中。

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UsernameTextField{
PasswordTextField.becomeFirstResponder()

} else if textField == PasswordTextField {
self.view.endEditing(true)
}
return true
}

let username = UsernameTextField.text // THIS IS NOT IN A FUNCTION

移到这里:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UsernameTextField{
PasswordTextField.becomeFirstResponder()
} else if textField == PasswordTextField {
self.view.endEditing(true)
}
let username = UsernameTextField.text // THIS IS NOW IN A FUNCTION
return true
}

关于swift - 读取 UITextfields 的值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49407444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com