How to use UISegmentedControl in guard together with textfields

53 Views Asked by At

I am currently facing a problem where I want to use guards to enable a button if desired information are filled out. After reading into the guard mechanism one thing is unclear for me. How do i combine UITextFields with UISegmentedControls so that both have to be filled out/selected in order to activate the button.

@objc func editingChanged(_ textField:UITextField, sgmntControl: UISegmentedControl) {
        if textField.text?.count == 1 {
            if textField.text?.first == " " {
                textField.text = ""
                return
            }
        }

        guard
            let name = tbxName.text, !name.isEmpty,
            let firstName = tbxFirstname.text, !firstName.isEmpty,
            let phoneNr = tbxPhoneNr.text, !phoneNr.isEmpty,
            let birthDate = tbxBirthdate.text, !birthDate.isEmpty,
            let gender = sgmntGender, !gender.isSelected,
            let street = tbxStreet.text, !street.isEmpty,
            let postalCode = tbxPLZ.text, !postalCode.isEmpty,
            let city = tbxCity.text, !city.isEmpty,
            let country = tbxCountry.text, !country.isEmpty,
            let idNumber = tbxIdNo.text, !idNumber.isEmpty,
            let valueCheck = sgmntValueCheck, !valueCheck.isSelected,
            let drivingSchoolFlyer = sgmntDrivingSchoolFlyer, !drivingSchoolFlyer.isSelected,
            let visionTest = sgmntVisionTest, !visionTest.isSelected,
            let visionHelp = sgmntVisionHelp, !visionHelp.isSelected
        else {
            rbbtnSave!.isEnabled = false
            return
        }
        rbbtnSave!.isEnabled = true
    }

I am currently not sure if my approach is correct, googling didn't help either. I thought that it would make sense to use the UISegmentedControl just like the text field, since both are checking for booleans with isSelected and isEmpty.

0

There are 0 best solutions below