首页 > 解决方案 > 如何检查文本字段是否存在 Xcode(以编程方式编码)

问题描述

我目前遇到了一个问题,我似乎无法通过将文本框链接到存在检查功能来解决这个问题。

我已经对文本框和界面进行了硬编码,而不是使用 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)
        }
    }
}

变量名称来自通过 Interface Builder 连接文本框时的名称,但在硬编码版本中它们是相同的,尽管该函数无法访问文本字段,我似乎无法找到解决方法。

任何帮助表示赞赏

标签: swiftxcodebuttontextfield

解决方案


您需要做的就是在当前方法之外声明您的文本字段,我认为这将是您的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)")
    }
}

推荐阅读