簡體   English   中英

如果所有 UITextField 都已填充且其中一個具有 Int 值,則啟用按鈕

[英]Enable a button if all the UITextFields are filled and one of them has an Int value

我將 textField 關聯到我的shouldChangeCharactersIn函數時遇到問題,並且此 textField 應包含 Int 值。 第一個 textField 應該包含 String 值,第二個應該包含 Int 值。 只有當 textFields 填充了正確的類型時,才應啟用確認按鈕。 那是我的應用程序: 在此處輸入圖像描述

和代碼:

import UIKit

class LoginViewController: UIViewController, UITextFieldDelegate {

   var av = ActualValues()
   var statsVC = StatsViewController()
   var closureBlock: (() -> Void)?

   @IBOutlet weak var idTextField: UITextField!
   @IBOutlet weak var idCheckButton: UIButton!
   @IBOutlet weak var segmentedControl: UISegmentedControl!
   @IBOutlet weak var targetsTextField: UITextField!

   override func viewDidLoad() {
      super.viewDidLoad()

      statsVC.allRunsFunc()
      idTextField.delegate = self
      idCheckButton.isEnabled = false
   }

   override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      av.shift = 1
      allRunsFunc()
   }

   @IBAction func idCheckButtonDidPress(_ sender: UIButton) {
      av.id = idTextField.text!
      av.databaseID = String(runs + 1)
      av.target = targetsTextField.text!
      closureBlock?()
      if let delegate = self.summaryButtonIsEnabledDelegate {
         delegate.toggleSummaryButton()
      }

      self.dismiss(animated: true, completion: nil)
   }

   @IBAction func segmentedControlChanged(_ sender: UISegmentedControl) {
      switch segmentedControl.selectedSegmentIndex {
      case 0:
        av.shift = 1
      case 1:
        av.shift = 2
      case 2:
        av.shift = 3
      default:
        av.shift = 1
      }
   }

   func configureID(model: ActualValues) {
     av = model
   }

   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String, int: Int) -> Bool {
      let text = (idTextField.text! as NSString).replacingCharacters(in: range, with: string)
      if text.isEmpty {
         idCheckButton.isEnabled = false
      } else {
         idCheckButton.isEnabled = true
      }

      return true
   }
}

我應該在該函數中添加什么?

嘗試創建一個 function 調用validator() 你所有的 textField 都應該被添加

textField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

@objc func textFieldDidChange(_ textField: UITextField) {
        // TODO : Validate all of your textField value here
}

將此@IBAction連接到兩個文本字段,用於Editing Changed

@IBAction func textFieldEdited(_ sender: Any) {

    // reset to disabled every time a text field value changes
    //  will be set to enabled if data is validated
    idCheckButton.isEnabled = false

    // make sure idTextField has text
    guard let idText = idTextField.text, idText != "" else {
        return
    }
    // make sure targetsTextField has text
    guard let targetsText = targetsTextField.text, targetsText != "" else {
        return
    }
    // make sure targetsTextField has only numbers
    guard targetsText.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil else {
        return
    }

    // enable the button
    idCheckButton.isEnabled = true

}

這將在任一字段中輸入的每個字符時調用。 它:

  • 首先將按鈕重置為.isEnabled = false
  • 然后檢查 id 文本字段是否有文本
  • 然后檢查目標文本字段是否有文本
  • 然后檢查目標字段是否只有數字

如果它通過所有 3 次檢查,它將按鈕設置為.isEnabled = true

代替

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String, int: Int) -> Bool {
   let text = (idTextField.text! as NSString).replacingCharacters(in: range, with: string)
   if text.isEmpty {
      idCheckButton.isEnabled = false
   } else {
      idCheckButton.isEnabled = true
   }

   return true
}

嘗試這個

// Add these variables
var idValid = false
var targetsValid = false

func textFieldDidChangeSelection(_ textField: UITextField) {
    if textField == idTextField {
         // Verify id has text
        if textField.text! != "" {
            idValid = true
        } else {
            idValid = false
        }
    }
    if textField == targetsTextField {
        // Verify targets is numeric
        if textField.text!.isNumeric {
            targetsValid = true
        } else {
            targetsValid = false
        }
    }
    if idValid && targetsValid {
        // Both valid enable
        idCheckButton.isEnabled = true
    } else {
        // Not both valid disable
        idCheckButton.isEnabled = false
    }
}

您必須為字符串添加此擴展名以驗證字符串是否為數值 - 將新的空白 Swift 文件添加到名為“StringExtension”的項目並添加:

import Foundation
import UIKit

extension String {
    var isNumeric: Bool {
        guard self.count > 0 else { return false }
        let nums: Set<Character> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
        return Set(self).isSubset(of: nums)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM