如何让两个变量相互依赖

时间:2021-12-02 13:15:37

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

我有这个问题:我有两个文本字段,一个包含数组的pickerView和另一个文本字段,您需要根据您在pickerView中所做的选择放入特定代码才能按下按钮。这就是我得到的

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

正如您所看到的,这只有在您选择选项1时才有效。如果您选择“选项1”并且在放入“zxy”的schoolCodeTxtField中,我将如何使其工作,它将继续,如果您选择“选项2”并输入“ gbv“它也将继续等等。我希望你明白我的意思。我感谢所有的帮助

3 个解决方案

#1


1  

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

就像SchoolArray一样,您也可以使用数组作为代码,并使用以下方法:

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

}

#2


0  

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 {

    }
}

#3


0  

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 {

    }
}

#1


1  

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

就像SchoolArray一样,您也可以使用数组作为代码,并使用以下方法:

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

}

#2


0  

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 {

    }
}

#3


0  

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 {

    }
}