从VC1传递UITextField(密码),并与VC2上的UITextField(确认密码)进行比较((是否相同))

时间:2018-12-23 23:07:35

标签: ios swift xcode uitextfield

ViewController2输入密码UITextField值需要传递到ViewController3

需要将ViewController3确认密码UITextField的值与ViewController2输入密码” {{ 1}},当用户单击“ 下一步UITextField时。

如果值不同,则需要在UIButton上方显示“ 密码不匹配UILabel,然后显示“ Next UITextField需要回到非活动状态。

目前,两个UIButton的标准都要求至少6个字符,因此“ Next UITextFields保持无效,直到满足条件。< / p>

ViewController2.swift

UIButton

ViewController3.swift:

import UIKit

class ViewController2: UIViewController, UITextFieldDelegate {

@IBOutlet weak var passwordField: UITextField!

@IBOutlet weak var toViewController3Button: UIButton!

@IBAction func backToViewController1(_ sender: Any) {
    print("back button pressed")
    self.performSegue(withIdentifier: "ViewController2ToViewController1Segue", sender: self)
}

@IBAction func toViewController3(_ sender: Any) {
    print("next button pressed")
    self.performSegue(withIdentifier: "ViewController2ToViewController3Segue", sender: self)
}

@IBAction func textFieldChangedValue(_ sender: UITextField) {
    toViewController3Button.isEnabled = sender.text!.count >= 6
}

override func viewDidLoad() {
    super.viewDidLoad()
    print("ViewController2 has loaded")
    // Is off until password criteria is satisfied
    toViewController3Button.isEnabled = false
    // Set delegate
    passwordField.delegate = self
    passwordField.becomeFirstResponder()
    passwordField.addTarget(self, action: #selector(textFieldChangedValue(_:)), for: .editingChanged)
}

// Hide keyboard when Return key is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    passwordField.resignFirstResponder()
    print("Keyboard Hidden by Return Key")
    return true
}

// Global: Hide Keyboard when screen is touched outside of text field
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
    print("Keyboard Hidden by Screen Tap")
}

// Confirm Password field must have 6 or more characters for Next -> button to activate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == passwordField {
        let countAfter = textField.text!.count + string.count - range.length
        toViewController3Button.isEnabled = countAfter >= 6
    }
    return true
}
}

或者,如果可以在用户向“ 确认密码”中输入字符时实时实时比较import UIKit class ViewController3: UIViewController, UITextFieldDelegate { @IBOutlet weak var confirmPasswordField: UITextField! @IBOutlet weak var toViewController4Button: UIButton! @IBAction func backToViewController2(_ sender: Any) { print("back button pressed") self.performSegue(withIdentifier: "ViewController3ToViewController2Segue", sender: self) } @IBAction func toViewController4(_ sender: Any) { print("next button pressed") self.performSegue(withIdentifier: "ViewController3ToViewController4Segue", sender: self) } @IBAction func textFieldChangedValue(_ sender: UITextField) { toViewController4Button.isEnabled = sender.text!.count >= 6 } override func viewDidLoad() { super.viewDidLoad() print("ViewController3 has loaded") // Is off until password criteria is satisfied toViewController4Button.isEnabled = false // Set delegate confirmPasswordField.delegate = self confirmPasswordField.becomeFirstResponder() confirmPasswordField.addTarget(self, action: #selector(textFieldChangedValue(_:)), for: .editingChanged) } // Hide keyboard when Return key is pressed func textFieldShouldReturn(_ textField: UITextField) -> Bool { confirmPasswordField.resignFirstResponder() print("Keyboard Hidden by Return Key") return true } // Global: Hide Keyboard when screen is touched outside of text field override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) print("Keyboard Hidden by Screen Tap") } // Confirm Password field must have 6 or more characters for Next -> button to activate func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if textField == confirmPasswordField { let countAfter = textField.text!.count + string.count - range.length toViewController4Button.isEnabled = countAfter >= 6 } return true } } 中的值UITextFields ,那么也很好(而不是使用“ Next UITextField发起比较)。

我太缺乏经验了,不知道哪条路线是最好的,所以当然也欢迎其他任何建议。

我能够将数据从VC1传递到VC2(如下面的代码片段所示),但无法弄清楚如何将其余数据挂钩。

ViewController1.swift:

UIButton

ViewController2.swift:

import UIKit
class ViewController1: UIViewController {

@IBOutlet weak var passwordField: UITextField!

var passwordText = ""

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func done(_ sender: Any) {
    self.passwordText = passwordField.text!
    performSegue(withIdentifier: "ViewController1ToViewController2", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var vc = segue.destination as! ViewController2
    vc.passwordFieldValue = self.passwordText
}
}

2 个答案:

答案 0 :(得分:1)

请勿绕过UITextField。而是准备一个模型。我猜您正在尝试实施注册流程。因此,如下创建注册请求模型。

struct SignupRequestModel {
    var email: String? // guessing you are using email 
    var password: String?
    var confirmedPassword: String?
    // ... other fields, which you require to complete signup
}

然后,当您绕过UITextField时,而不是传递SignupRequestModel的实例,并逐渐设置其所有属性。

希望您能理解我的观点。

快乐的编码。

答案 1 :(得分:0)

不要将UITextField传递到下一个ViewController。您可以传递textField.text自己。

  

您将比较两个字符串。如果不匹配,您可以弹出   回来。

因此,您可以更改我们的代码,例如 ViewController2

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

     let conformViewController = segue.destination as! ViewController3
     conformViewController.password = passwordTextField.text ?? ""
    }

在ViewController3中

var password: String = ""

然后您可以在ViewController3中进行比较

let confirmPassword = confirmPasswordField.text ?? ""


if password == confirmPassword {
nextButton.isEnabled = true
} else {
label.text=“password mismatched”
}