我目前遇到了一个问题,我似乎无法解决将文本框链接到状态检查函数的问题。
我已经对文本框和界面进行了硬编码,而不是使用Xcode提供的故事板界面来进一步提高我的技能,但我被如何绕过这一点所困扰。在ViewDidLoad中为按钮和文本字段设置GUI的功能是:
//making the Email text equal to the custom Hoshi Text Field - PasswordText field is the same
let EmailAddressText = HoshiTextField()
//setting the text field to not have auto constraints put in, so that constraints can be put in later
EmailAddressText.translatesAutoresizingMaskIntoConstraints = false
//setting the placeholder text colour to black
EmailAddressText.placeholderColor = .black
//setting the placeholder text to be equal to Email Address
EmailAddressText.placeholder = "Email Address"
//setting the border active and inactive colour
EmailAddressText.borderActiveColor = .systemGreen
EmailAddressText.borderInactiveColor = .systemGray5
//setting the delegate
EmailAddressText.delegate = self
//adding the text box to the temporary view
view.addSubview(EmailAddressText)
//adding in the Login Button
let LoginButton = UIButton()
//setting the Button to not have auto constraints put in, so that constraints can be put in later
LoginButton.translatesAutoresizingMaskIntoConstraints = false
//setting the title for the button when it is in the normal condition
LoginButton.setTitle("Login", for: .normal)
//setting the colour for the button
LoginButton.setTitleColor(.white, for: .normal)
//set the background colour so it stands out from the background
LoginButton.backgroundColor = .black
//setting it to have round corners
LoginButton.layer.cornerRadius = 15
//adding the button to the temp view
view.addSubview(LoginButton)
//setting the function which will be called when the button is pressed
LoginButton.addTarget(self, action: #selector(loginPressed), for: .touchUpInside)
//setting the view with the label and constraints to the original view
self.view = view
约束是使用NSConstraint.active功能完成的,函数loginPressed((的代码是:
//setting the login pressed function
@objc private func loginPressed() {
guard let Email = EmailAddressText.text, EmailAddressText.text?.count != 0 else {
AlertService.showAlert(style: .alert, title: "Error", message: "Please enter a valid email address and try again")
return
}
guard let Password = PasswordText.text, PasswordText.text?.count != 0 else {
AlertService.showAlert(style: .alert, title: "Error", message: "Please enter the correct password and try again")
return
}
Auth.auth().signIn(withEmail: Email, password: Password) { (data, err) in
if let err = err {
AlertService.showAlert(style: .alert, title: "Login error", message: "Error code: (err.localizedDescription) Please try again later")
} else {
//login success
UserDefaults.standard.setValue(true, forKey: "LoginBool")
self.performSegue(withIdentifier: "LoginSuccess", sender: self)
}
}
}
变量名来自通过接口生成器连接文本框时的名称,但在硬编码版本中是相同的,尽管函数无法访问文本字段,我似乎找不到解决方法。
感谢提供的任何帮助
您所需要做的就是在当前方法之外声明您的文本字段,我认为它将是您的viewDidLoad()
。目前,您的文本字段和按钮只能在创建它们的方法的范围内直接访问,通过将文本字段的声明移动到控制器类,您可以直接从控制器中的任何位置访问文本字段。此外,Swift建议的对象实例(例如文本字段(的命名约定是以小写字母开头的大小写,即emailAddressText
而不是EmailAddressText
。
class ViewController: UIViewController {
let emailAddressText = HoshiTextField()
override func viewDidLoad() {
super.viewDidLoad()
//setting the text field to not have auto constraints put in, so that constraints can be put in later
emailAddressText.translatesAutoresizingMaskIntoConstraints = false
//setting the placeholder text colour to black
emailAddressText.placeholderColor = .black
//setting the placeholder text to be equal to Email Address
emailAddressText.placeholder = "Email Address"
//setting the border active and inactive colour
emailAddressText.borderActiveColor = .systemGreen
emailAddressText.borderInactiveColor = .systemGray5
//setting the delegate
emailAddressText.delegate = self
//adding the text box to the temporary view
view.addSubview(emailAddressText)
// rest of your code
// ...
// ...
}
@objc private func loginPressed() {
// small tip: A variable created in a guard statement can be used later in the same guard statment
guard let email = emailAddressText.text, email.count != 0 else {
return
}
print("Email: (email)")
}
}