Crash on pushViewController

2.6k views Asked by At

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: enter image description here

The stack trace: enter image description here

3

There are 3 answers

3
Sulthan On BEST ANSWER

My guess is that your navigationController is nil. Forcing it to unwrap using ! will then result in a crash. Make sure that navigationController is not nil.

1
Özgür Ersil On

my advice try to push new view controller in dismissView compliation,

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

I know the problem is solved but in my case: I was copied a button from one controller storyboard to another, and the button outlet was copied with it to new controller and caused the crash.