关闭。这个问题需要
details or clarity .它目前不接受答案。
Best Answer-推荐答案 strong>
不要忘记导入 UITextFieldDelegate
,如下面的代码所示。你可以这样做:
class File2: UIViewController, UITextFieldDelegate{
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var label: UILabel!
var i = 0
var password = ""
var confirmPassword = ""
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.secureTextEntry = true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
if newLength == 4{
if i == 0{
password = textField.text!
textField.text = ""
textField.placeholder = "Confirm Password"
i+=1
}
else if i == 1{
confirmPassword = textField.text!
textField.text = ""
i = 0
if confirmPassword == password{
// save the password... (Your Code)
// I know userdefaults is not a good way to store passwords, but for tutorial purposes, it's OK.
NSUserDefaults.standardUserDefaults.setValue(password, forKey: "password")
print("Account Succesfully Created With Password: \(password)!")
}
}
}
return newLength <= 4
return true
}
}
读取密码:
查看 Stack Overflow 上的 UserDefaults
文档。
关于ios - 在 iOS 中验证单个文本字段中的两个值,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/38851895/