【发布时间】:2021-01-21 16:46:22
【问题描述】:
我目前遇到一个问题,我似乎无法解决将文本框链接到存在检查功能的问题。
我已经对文本框和界面进行了硬编码,而不是使用 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)
}
}
}
变量名称来自通过界面生成器连接文本框时,但在硬编码版本中它们是相同的,尽管该函数无法访问文本字段,我似乎无法找到解决方法.
感谢任何帮助
【问题讨论】:
-
请进一步解释您对“该函数无法访问文本字段”的含义。
-
我的意思是按下按钮时调用的函数(loginPressed)找不到文本字段(EmailAddress 和 PasswordText),所以目前它找不到任何文本字段来进行存在检查上
标签: swift xcode button textfield