2017-09-19 51 views
0

我有我的自定義導航欄中的菜單tableview。如果我點擊一個單元格,它應該帶我到另一個視圖控制器。但是當我點擊它時,我的應用程序崩潰了。storyboard?.instantiateViewController(withIdentifier:「」)as! viewcontroller

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return menu_items.count //It has 5 values 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell") 

    cell.textLabel?.text = menu_items[indexPath.row] 
    cell.textLabel?.textColor = .white 

    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 


    let item = menu_items[indexPath.row] 

    if (item == "Patienten") { 


     tableView.removeFromSuperview() 

     let vc = self.storyboard?.instantiateViewController(withIdentifier: "patientView") as! patientViewVC //App crashes here 
     self.present(vc, animated: false, completion: nil) 

    } else if (item == "Mitarbeiter") { 


    } else if (item == "Kalender") { 


    } else if (item == "Tourenplanung") { 


     tableView.removeFromSuperview() 

     let vc = storyboard?.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here 
     self.present(vc, animated: false, completion: nil) 


    }else if (item == "Abmelden") { 

     let vc = self.storyboard?.instantiateViewController(withIdentifier: "loginvc") as! LoginVC //App crashes here 
     self.present(vc, animated: false, completion: nil) 
    } 


    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! 
    selectedCell.contentView.backgroundColor = colorLightGreen 

} 

}

我曾無數次驗證所有標識符名稱和視圖控制器的名字是正確的。

+2

它崩潰?控制檯中的錯誤消息是什麼? 'UIViewController'是否與當前的故事板一樣? – Larme

+0

你確定你有'patientViewVC'在tableview是相同的故事板? –

+0

@Larme我在控制檯中看不到任何錯誤消息,我在該行看到EXC_BAD_INSTRUCTION。 – swiftuser123

回答

0

確保在實例化viewController之前加載正確的故事板。上述

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let item = menu_items[indexPath.row] 

    if (item == "Patienten") { 
     tableView.removeFromSuperview() 

     let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil) 
     let vc = storyBoard.instantiateViewController(withIdentifier: "patientView") as! patientViewVC 
     self.present(vc, animated: false, completion: nil) 

    } else if (item == "Mitarbeiter") { 


    } else if (item == "Kalender") { 


    } else if (item == "Tourenplanung") { 


     tableView.removeFromSuperview() 
     let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil) 
     let vc = storyBoard.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here 
     self.present(vc, animated: false, completion: nil) 


    }else if (item == "Abmelden") { 
     let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil) 
     let vc = storyBoard.instantiateViewController(withIdentifier: "loginvc") as! LoginVC //App crashes here 
     self.present(vc, animated: false, completion: nil) 
    } 


    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! 
    selectedCell.contentView.backgroundColor = colorLightGreen 

} 

STORYBOARDNAME是包含patientViewVC,tourVC故事板的名稱,LoginVC

+0

哇,你救了我! '讓storyBoard = UIStoryboard(名稱:「StoryboardName」,捆綁:無)「像魅力一樣工作! – swiftuser123

+0

@ swiftuser123:隨時歡迎好友:)快樂編碼 –

0

在故事板視圖控制器需要有一個自定義類集身份檢查:

enter image description here

然後你就可以將它轉換爲所需的類型。如果所有視圖控制器都在同一個故事板中,則可以使用storyboard屬性將它們實例化。如果它們在不同的故事板中,則必須使用相應的名稱實例化UIStoryboard實例並使用它。

順便說一句,你爲什麼在電池選擇後打電話tableView.removeFromSuperview()

相關問題