Cannot embed in navigation controller if I pass data using prepareForSegue

I am working on a custom camera application where I present 3 viewing modes. At each step, I pass data using the prepareForSegue function. My problem is that after completing the work with the camera, I need to show 2 more viewing modes that should be inside the navigation controller.

I realized that if I do not transmit any data, the navigation controller works fine. However, when I transfer data, the application crashes at runtime. What is the right way to do this?

Here is my preparation for the segue function;

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "camera2Crop"{
        let controller: CropViewController = segue.destinationViewController as CropViewController
        controller.photoTaken = self.photoTaken
    }
}

where photoTakenis the object UIImage. Moreover, here is a screenshot from my storyboard where I put the navigationController. I call the function prepareForSeguein CustomCameraViewControllerto go to CropViewController.

Storyboard Screenshot

EDIT: I changed my preparation method for the following code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "camera2Crop" {
        let controller: CustomNavigationController = segue.destinationViewController as CustomNavigationController
        controller.photoTaken = self.photoTaken

    }
}

Now the application does not crash, but I do not know how to send an object through the navigation controller

+4
source share
1 answer
let controller: CropViewController = segue.destinationViewController as CropViewController

Double check if segue.destinationViewControllerit is actually a navigation manager.

If it is a navigation controller, get CropViewControllerfrom it:

if segue.identifier == "camera2Crop" {
    let navController = segue.destinationViewController as UINavigationController
    let controller = navController.viewControllers[0] as CropViewController
    controller.photoTaken = self.photoTaken
}

Note that you do not need to subclass the UINavigationController.

+10

All Articles