菜鸟教程小白 发表于 2022-12-11 17:13:23

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 = &#34;&#34;
var confirmPassword = &#34;&#34;

override func viewDidLoad() {
    super.viewDidLoad()
    textField.delegate = self
    textField.secureTextEntry = true
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
    let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
    if newLength == 4{
      if i == 0{
            password = textField.text!
            textField.text = &#34;&#34;
            textField.placeholder = &#34;Confirm Password&#34;
            i+=1
      }
      else if i == 1{
            confirmPassword = textField.text!
            textField.text = &#34;&#34;
            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&#39;s OK.
                NSUserDefaults.standardUserDefaults.setValue(password, forKey: &#34;password&#34;)
                print(&#34;Account Succesfully Created With Password: \(password)!&#34;)
            }
      }
    }
    return newLength &lt;= 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]
查看完整版本: ios - 在 iOS 中验证单个文本字段中的两个值