的行中
我有一个带有单元格的表视图。当我按下单元格时它不显示第二个视图控制器当我按下另一个单元格时,它给了我这个错误:
致命错误:在展开可选值时意外发现nil(lldb)
这是我的代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toMediaPlayer" {
let VC = segue.destinationViewController as! UIViewController
let indexPath: NSIndexPath = tableView.indexPathForSelectedRow()!
VC.title = songs[indexPath.row]
}
我建议使用indexPathForCell()
而不是indexPathForSelectedRow()
来获取索引路径。适当的单元格作为sender
参数传递
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toMediaPlayer" {
if sender is UITableViewCell {
let VC = segue.destinationViewController as! UIViewController
let indexPath = tableView.indexPathForCell(sender)
VC.title = songs[indexPath.row]
}
}
我怀疑错误发生在获取destinationViewController