在pushViewController上崩溃

时间:2015-06-15 11:39:09

标签: ios swift viewcontroller pushviewcontroller

我试图推送一个ViewController,但我的应用每次都会崩溃。

我使用UIImagePickerControllerDelegate拍照,拍完照片后,这段代码就会运行:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let pickedImage = info[UIImagePickerControllerEditedImage] as! UIImage

    // Reset the cameraButton and Tabbar
    self.cameraButton.setImage(UIImage(named: "camera-button"), forState: .Normal)
    self.cameraButton.transform = CGAffineTransformMakeScale(1, 1)
    self.tabBarController?.tabBar.alpha = 1

    let realmImage = Image()
    realmImage.imagedata = UIImageJPEGRepresentation(pickedImage, 1.0)

    let birthmark = Birthmark()
    //birthmark.imagesArray = RLMArray()
    birthmark.imagesArray.addObject(realmImage)
    birthmark.bodyPart = Birthmark.Bodypart.LeftArm

    realm.beginWriteTransaction()
    realm.addObject(birthmark)
    realm.commitWriteTransaction()

    self.dismissViewControllerAnimated(true, completion: nil)

    let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemViewController") as! ItemViewController
    secondViewController.existingItem = birthmark
    navigationController!.presentViewController(secondViewController, animated: true, completion: nil)
}

然而,它总是在最后一行崩溃:

navigationController!.presentViewController(secondViewController, animated: true, completion: nil)

XCode说:EXC_BREAKPOINT(代码= 1,子代码= 0x100489474)

你对我做错了什么有任何指示吗?

这是我的故事板的样子: enter image description here

堆栈跟踪: enter image description here

3 个答案:

答案 0 :(得分:0)

我的建议是尝试在dismissView compliation中推送新的视图控制器,

self.dismissViewControllerAnimated(true, completion: {
     let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemViewController") as! ItemViewController
     secondViewController.existingItem = birthmark
     navigationController!.presentViewController(secondViewController, animated: true, completion: nil)
});

答案 1 :(得分:0)

我猜您的navigationControllernil。强制使用!解包将导致崩溃。确保navigationController不是nil

答案 2 :(得分:0)

我知道问题已解决,但就我而言:将按钮从一个控制器情节提要板复制到另一个控制器,然后将按钮出口与其一起复制到新控制器,并导致崩溃。