ios - 在 iOS 中验证单个文本字段中的两个值
<p><div>
<aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
<div class="d-flex fd-column fw-nowrap">
<div class="d-flex fw-nowrap">
<div class="flex--item wmn0 fl1 lh-lg">
<div class="flex--item fl1 lh-lg">
<b>关闭</b>。这个问题需要<a href="https://stackoverflow.com/help/closed-questions" rel="noreferrer noopener nofollow">details or clarity</a> .它目前不接受答案。
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不要忘记导入 <code>UITextFieldDelegate</code>,如下面的代码所示。你可以这样做:</p>
<pre><code>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
}
}
</code></pre>
<h2>读取密码:</h2>
<p>查看 <strong>Stack Overflow</strong> 上的 <code>UserDefaults</code> 文档。</p></p>
<p style="font-size: 20px;">关于ios - 在 iOS 中验证单个文本字段中的两个值,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38851895/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38851895/
</a>
</p>
页:
[1]