当我点击我的详细披露按钮时,它崩溃了我的应用程序

时间:2015-08-05 05:48:03

标签: ios iphone xcode swift

我创建了一个类似其余的应用程序,但每当我点击详细信息披露来编辑现有列表名称的名称时,突然它会崩溃应用程序。我在班上写了一个accessoryButtonTappedForRowWithIndexPath的代码。下面是我的accessoryButtonTappedForRowWithIndexPath方法的代码。

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath)
{
    let navigationController = storyboard!.instantiateViewControllerWithIdentifier("ListDetailViewController") as! UINavigationController

    let controller = navigationController.topViewController as! ListDetailViewController

    controller.delegate = self

    let checklist = dataModel.lists[indexPath.row]

    controller.checklistToEdit = checklist

    presentViewController(navigationController, animated: true, completion: nil)
    }

2 个答案:

答案 0 :(得分:0)

您的应用程序崩溃,因为其中一个变量为nil:

  • 故事板
  • navigationController.topViewController

或者您在此处收到index out of bounds错误

dataModel.lists[indexPath.row]

从你的代码来看,这一行也不明显,如果这一行:

storyboard!.instantiateViewControllerWithIdentifier("ListDetailViewController") as! UINavigationController

确实应该做它应该做的事情,因为你的字符串显示,你想要一个ListDetailViewController,但你将函数的结果转换为UINavigationViewController,这也可能导致崩溃。

修复崩溃并了解正在进行的重写代码(仅用于测试purporse,因为此代码很难看):

if let navigationController = storyboard?.instantiateViewControllerWithIdentifier("ListDetailViewController") as! UINavigationController 
{
  if let controller = navigationController.topViewController as? ListDetailViewController
  {
    controller.delegate = self
    if dataModel.list.count < indexPath.row
    {
      let checklist = dataModel.lists[indexPath.row]
      controller.checklistToEdit = checklist
      presentViewController(navigationController, animated: true, completion: nil) 
    } else 
    {
       println("array out of index")
    }
  } else 
  {
     println("topView Controller of navigation controller is nil")
  }
} else 
{
  println("navigationController or storyboard is nil")
}

答案 1 :(得分:0)

希望这会对你有所帮助。