如何检查多个文本字段是否为 nil,并相应地在 Swift 中启用按钮

huangapple go评论63阅读模式
英文:

How to check multiple textfields are nil or not and enable button accordingly in swift

问题

需要在所有文本字段都有文本时才能启用 nextBtn

尝试以下代码:但是使用此代码,当我点击第一个文本字段时,nextBtn 就会启用,为什么呢?如何只在填写完所有文本字段后才启用 nextBtn?请指导我。

override func viewDidLoad() {
    super.viewDidLoad()
    
    nextBtn.isEnabled = false
    nextBtn.alpha = 0.5
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    
    if fullNameTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if dateOfBirthTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if schoolNameTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if schoolLocationTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if gradeClassTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else{
        nextBtn.alpha = 1
        nextBtn.isEnabled = true
    }
}

编辑:我尝试了这样做。现在,在第二个文本字段上点击后,nextBtn 变为可用,为什么呢?请指导我。

func textFieldDidBeginEditing(_ textField: UITextField) {

    if fullNameTextField.text == "", dateOfBirthTextField.text == "", schoolNameTextField.text == "", gradeClassTextField.text == ""{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else{
        nextBtn.alpha = 1
        nextBtn.isEnabled = true
    }
}
英文:

I need to enable nextBtn when all textfields has some text only

tried below code: but with this code when i tap on first textfield then my nextBtn becomes enabled why? how to make nextBtn enable after filling the all textfields only. please guide me

override func viewDidLoad() {
    super.viewDidLoad()
    
    nextBtn.isEnabled = false
    nextBtn.alpha = 0.5
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    
    if fullNameTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if dateOfBirthTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if schoolNameTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if schoolLocationTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else if gradeClassTextField.text == nil{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else{
        nextBtn.alpha = 1
        nextBtn.isEnabled = true
    }
}

edit: i have tried like this. now after taping on second textfield then nextBtn becomes enable why? please guide me

func textFieldDidBeginEditing(_ textField: UITextField) {

    if fullNameTextField.text == "", dateOfBirthTextField.text == "", schoolNameTextField.text == "", gradeClassTextField.text == ""{
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false
    }
    else{
        nextBtn.alpha = 1
        nextBtn.isEnabled = true
    }
}

答案1

得分: 1

你可以通过以下方式简单地完成它:

let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField, schoolLocationTextField, gradeClassTextField]
let isValidFields = fields.allSatisfy { field in
    !field.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
if isValidFields {
    nextBtn.alpha = 1
    nextBtn.isEnabled = true
} else {
    nextBtn.alpha = 0.5
    nextBtn.isEnabled = false
}
英文:

You can simply do it by doing it like this

let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField, schoolLocationTextField, gradeClassTextField]
let isVaildFields = fields.allSatisfy { field in
            !field.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
         } 
if isVaildFields {
    nextBtn.alpha = 1
    nextBtn.isEnabled = true
} else {
    nextBtn.alpha = 0.5
    nextBtn.isEnabled = false
}

答案2

得分: 0

以下是翻译好的代码部分:

一种方法是这样的

    class ViewController: UIViewController, UITextFieldDelegate {
        
        @IBOutlet weak var fullNameTextField: UITextField!
        @IBOutlet weak var dateOfBirthTextField: UITextField!
        @IBOutlet weak var schoolNameTextField: UITextField!
        @IBOutlet weak var schoolLocationTextField: UITextField!
        @IBOutlet weak var gradeClassTextField: UITextField!
        @IBOutlet weak var nextBtn: UIButton!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            nextBtn.isEnabled = false
            nextBtn.alpha = 0.5
        }
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField,
                          schoolLocationTextField, gradeClassTextField]
            let isValidFields = fields.allSatisfy {
                !($0?.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
            }
            
            if isValidFields {
                nextBtn.alpha = 1
                nextBtn.isEnabled = true
            } else {
                nextBtn.alpha = 0.5
                nextBtn.isEnabled = false
            }
        }
    }

你也可以通过创建文本字段 `@IBOutlet` 的列表来实现,如下所示:

    class ViewController: UIViewController, UITextFieldDelegate {
        
        @IBOutlet var textFields: [UITextField]!
        @IBOutlet weak var nextBtn: UIButton!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            nextBtn.isEnabled = false
            nextBtn.alpha = 0.5
        }
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            let isValidFields = textFields.allSatisfy {
                !($0.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
            }
            
            if isValidFields {
                nextBtn.alpha = 1
                nextBtn.isEnabled = true
            } else {
                nextBtn.alpha = 0.5
                nextBtn.isEnabled = false
            }
        }
    }

如果你只想对单独的文本字段进行操作,可以简单地这样做。

    class ViewController: UIViewController, UITextFieldDelegate {
        
        @IBOutlet weak var fullNameTextField: UITextField!
        @IBOutlet weak var dateOfBirthTextField: UITextField!
        @IBOutlet weak var schoolNameTextField: UITextField!
        @IBOutlet weak var schoolLocationTextField: UITextField!
        @IBOutlet weak var gradeClassTextField: UITextField!
        @IBOutlet weak var nextBtn: UIButton!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            nextBtn.isEnabled = false
            nextBtn.alpha = 0.5
        }
        
        func isValid(_ textField: UITextField) -> Bool {
            return !(textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
        }
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            if isValid(fullNameTextField), isValid(dateOfBirthTextField),
               isValid(schoolNameTextField), isValid(schoolLocationTextField),
               isValid(gradeClassTextField) {
                nextBtn.alpha = 1
                nextBtn.isEnabled = true
            } else {
                nextBtn.alpha = 0.5
                nextBtn.isEnabled = false
            }
        }
    }
英文:

One way of doing it is like

class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var fullNameTextField: UITextField!
@IBOutlet weak var dateOfBirthTextField: UITextField!
@IBOutlet weak var schoolNameTextField: UITextField!
@IBOutlet weak var schoolLocationTextField: UITextField!
@IBOutlet weak var gradeClassTextField: UITextField!
@IBOutlet weak var nextBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nextBtn.isEnabled = false
nextBtn.alpha = 0.5
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let fields = [fullNameTextField, dateOfBirthTextField, schoolNameTextField,
schoolLocationTextField, gradeClassTextField]
let isValidFields = fields.allSatisfy {
!($0?.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
}
if isValidFields {
nextBtn.alpha = 1
nextBtn.isEnabled = true
} else {
nextBtn.alpha = 0.5
nextBtn.isEnabled = false
}
}
}

You can also do it by creating list of text field @IBOutlet, Like following:

class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textFields: [UITextField]!
@IBOutlet weak var nextBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nextBtn.isEnabled = false
nextBtn.alpha = 0.5
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let isValidFields = textFields.allSatisfy {
!($0.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
}
if isValidFields {
nextBtn.alpha = 1
nextBtn.isEnabled = true
} else {
nextBtn.alpha = 0.5
nextBtn.isEnabled = false
}
}
}

And if you only want to do it for individual textfields, you can simply do like this.

class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var fullNameTextField: UITextField!
@IBOutlet weak var dateOfBirthTextField: UITextField!
@IBOutlet weak var schoolNameTextField: UITextField!
@IBOutlet weak var schoolLocationTextField: UITextField!
@IBOutlet weak var gradeClassTextField: UITextField!
@IBOutlet weak var nextBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nextBtn.isEnabled = false
nextBtn.alpha = 0.5
}
func isValid(_ textField: UITextField) -> Bool {
return !(textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if isValid(fullNameTextField), isValid(dateOfBirthTextField),
isValid(schoolNameTextField), isValid(schoolLocationTextField),
isValid(gradeClassTextField) {
nextBtn.alpha = 1
nextBtn.isEnabled = true
} else {
nextBtn.alpha = 0.5
nextBtn.isEnabled = false
}
}
}

huangapple
  • 本文由 发表于 2023年6月15日 19:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定