简体   繁体   中英

How to have two variables depending on each other

I have this problem: I have two text fields, one pickerView containing an array and another text field where you need to put in a specific code depending on the selection you made in the pickerView to be able to press the button. This is what I got

var SchoolsArray = ["Option 1",
                "Option 2",
                "Option 3",
                "Option 4"]

var code1 = "zxy" // code for Option 1
var code2 = "gbv" // code for Option 2
var code3 = "jwn" // code for Option 3
var code4 = "hqc" // code for Option 4

@IBOutlet weak var firstNameTxtField: UITextField!
@IBOutlet weak var schoolNameTxtField: UITextField!
@IBOutlet weak var schoolCodeTxtField: UITextField!

@IBAction func createAccountBtnPressed(_ sender: Any) {
    if firstNameTxtField.text != nil && schoolNameTxtField.text != nil && schoolCodeTxtField.text != nil {
        if schoolNameTxtField.text == "Option 1" && schoolCodeTxtField.text == code1  {
            //do something here
        } else {

        }
    } else {

    }
}

As you can see this only works if you select Option 1. How can I make this work so if you select "Option 1" and in schoolCodeTxtField put in "zxy" it will proceed and if you select "Option 2" and put in "gbv" it will also proceed and so on. I hope you understand what I mean. I appreciate all help

Just like SchoolArray , you can use an array for the codes as well, and use following method:

var CodesArray = ["zxy", "gbv", "jwn", "hqc"]

@IBAction func createAccountBtnPressed(_ sender: Any) {
    guard
        firstNameTxtField.text != nil,
        let option = schoolNameTxtField.text,
        let index = SchoolsArray.index(where: { $0 == option }),
        CodesArray[index] == schoolCodeTxtField.text
    else {
        return
    }
    // Code & Option both matched

}

How about use a dictionary that contains your options as keys and your codes as values. Something like this:

var SchoolsOptions = ["Option 1": "zxy",
            "Option 2": "gbv",
            "Option 3": "jwn",
            "Option 4": "hqc"]

@IBAction func createAccountBtnPressed(_ sender: Any) {
    if firstNameTxtField.text != nil && schoolNameTxtField.text != nil && schoolCodeTxtField.text != nil {
        for (option, code) in SchoolsOptions {
            if schoolNameTxtField.text == option && schoolCodeTxtField.text == code  {
                //do something here
                // You only get here if the option and code match for that given school. If you need specific logic for each school you'll have to check which option you're on.
            }
        }
    } else {

    }
}

This is the cleanest solution I can think of right off the bat.

@IBOutlet weak var firstNameTxtField: UITextField!
@IBOutlet weak var schoolNameTxtField: UITextField!
@IBOutlet weak var schoolCodeTxtField: UITextField!

var codeArray: [String] = [
    "zxy",
    "gbv",
    "jwn",
    "hqc"
]

var selectedCode: String!

override func viewDidLoad() {
    super.viewDidLoad()
    setupPickerViewAndAssignItsDelegateAndDatasource()
    guard let selectedCode = codeArray.first else { return }
    self.selectedCode = selectedCode
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return codeArray.count
}

public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return "Option \(row + 1)"
}

public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedCode = codeArray[row]
}

@IBAction func createAccountBtnPressed(_ sender: Any) {
    if firstNameTxtField.text != nil && schoolNameTxtField.text != nil && schoolCodeTxtField.text != nil {
        if schoolNameTxtField.text == selectedCode {
            //do something here
        } else {

        }
    } else {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM