I'm trying to push a ViewController, but my app crashes every time.
I'm using the UIImagePickerControllerDelegate to take a picture, and once the picture has been taken, this code runs:
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)
}
However, it always crashes on the last line:
navigationController!.presentViewController(secondViewController, animated: true, completion: nil)
XCode says: EXC_BREAKPOINT (code=1, subcode=0x100489474)
Do you have any pointers as to what I'm doing wrong?
Here's what my storyboard looks like:
The stack trace:
My guess is that your
navigationController
isnil
. Forcing it to unwrap using!
will then result in a crash. Make sure thatnavigationController
is notnil
.