使用UIPicker执行segue的if语句

时间:2019-06-12 17:45:55

标签: arrays swift iphone if-statement arraylist

我正在构建一个应用程序,在某个问题上,我可以使用UIPicker返回到先前的问题,但是尝试使用该应用程序时,我得到了一个错误或没有任何反应。

此外,我所有页面的Storyboard ID都是我为segue命名的名称。

我尝试使用switch语句,但是出现了太多错误,因此我决定if语句可能会更好。

旧开关语句:

    public func textFieldDidEndEditing(_ textField: UITextField) {

    let row = picker.selectedRow(inComponent: 0)

    switch (row) {

    case 0: storyboard?.instantiateViewController(withIdentifier: "Q1") as? Q1; performSegue(withIdentifier: "Q1", sender: self)
    break;
    case 1: storyboard?.instantiateViewController(withIdentifier: "Q2") as? Q2; performSegue(withIdentifier: "Q2", sender: self)
    break;
    case 2: storyboard?.instantiateViewController(withIdentifier: "Q3") as? Q3; performSegue(withIdentifier: "Q3", sender: self)
    break;
    case 3: storyboard?.instantiateViewController(withIdentifier: "Q4") as? Q4; performSegue(withIdentifier: "Q4", sender: self)
    break;
    case 4: storyboard?.instantiateViewController(withIdentifier: "Q5") as? Q5; performSegue(withIdentifier: "Q5", sender: self)
    break;
    case 5: storyboard?.instantiateViewController(withIdentifier: "Q6") as? Q6; performSegue(withIdentifier: "Q6", sender: self)
    break;
    default: break;
}
}

我得到的错误是“使用未声明的类型“ Q1””

我正在尝试做一些类似的事情,

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, 
inComponent component: Int) {
        DiffQuestion.text = data[row]

        if (data == "Q1") {
        storyboard?.instantiateViewController(withIdentifier: "Q1") as? Q1; 
    performSegue(withIdentifier: "Q1", sender: self)
       }
    }

当前错误在我的if语句上“二进制运算符'=='无法应用于类型'[String]'和'String'的操作数” 我知道我可能把它的结构弄错了,我对这一切仍然很陌生。

更新: 我目前将其写为:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    DiffQuestion.text = data[row]

    if (data[row] == "Q1") {
        storyboard?.instantiateViewController(withIdentifier: "Q1"); performSegue(withIdentifier: "Q1", sender: self)
    }
    if (data[row] == "Q2") {
        storyboard?.instantiateViewController(withIdentifier: "Q2"); performSegue(withIdentifier: "Q2", sender: self)
    }
    if (data[row] == "Q3") {
        storyboard?.instantiateViewController(withIdentifier: "Q3"); performSegue(withIdentifier: "Q3", sender: self)
    }
    if (data[row] == "Q4") {
        storyboard?.instantiateViewController(withIdentifier: "Q4"); performSegue(withIdentifier: "Q4", sender: self)
    }
    if (data[row] == "Q5") {
        storyboard?.instantiateViewController(withIdentifier: "Q5"); performSegue(withIdentifier: "Q5", sender: self)
    }
    if (data[row] == "Q6") {
        storyboard?.instantiateViewController(withIdentifier: "Q6"); performSegue(withIdentifier: "Q6", sender: self)
    }

第6季度和第4季度一样都有过渡,但是其他所有内容都给了我“线程1:SIGABRT”

调试区显示:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'接收器()没有标识为'Q5'的标记

1 个答案:

答案 0 :(得分:0)

问题在这里

if (data == "Q1") { // == can't be used to compare String and [String]

替换为

if data[row] == "Q1" { }

此外,您应该只创建并使用segue

performSegue(withIdentifier: "Q1", sender: self)

OR 当前/推送

 let vc =  storyboard?.instantiateViewController(withIdentifier: "Q1") as? Q1
 // present or push vc
  self.present(vc, animated: true, completion: nil)
/// or push
 self.navigationController?.pushViewController(vc, animated: true)