如何让两个变量相互依赖

时间:2018-01-09 19:17:13

标签: arrays swift button data-structures

我遇到了这个问题:我有两个文本字段,一个包含数组的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 {

    }
}

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

3 个答案:

答案 0 :(得分:1)

就像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

}

答案 1 :(得分:0)

如何使用包含选项的字典作为键,将代码作为值。像这样:

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 {

    }
}

答案 2 :(得分:0)

这是我能想到的最干净的解决方案。

@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 {

    }
}