我正在使用Swift,我在tableview的didSelectRowAtIndexPath方法中出現錯誤。 我想傳遞一個值給另一個視圖控制器,即'secondViewController'。這裏EmployeesId是一個數組。相關代碼如下:在Swift中將值從一個視圖控制器傳遞給另一個視圖
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var view: Dashboard = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as Dashboard
self.navigationController?.pushViewController(view, animated: true)
secondViewController.UserId = employeesId[indexPath.item] //getting an error here.
}
但我收到此錯誤: 致命錯誤:意外發現零而展開的可選值。
任何幫助將不勝感激。
是什麼類型用戶ID?如果它是UILabel,那麼在視圖呈現之前它將爲零。另外,secondViewController的設置在哪裏? – 2014-10-31 14:43:07
它應該是indexPath.row,而不是indexPath.item,但我不知道這是否能解決您的問題。你確定employeesId [indexPath.item]不是零嗎? – rdelmar 2014-10-31 14:58:10
是seconfViewController中的UserId(強,非原子)? – casillas 2014-10-31 15:41:17