Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't embed in navigation controller if I am passing data with prepareForSegue

I am working on a custom camera app where I present 3 viewControllers modally. At each segue, I pass data with prepareForSegue function. My problem is after the work with camera is finished, I need to show 2 more viewControllers which need to be inside a navigationController.

I have realized that If I don't pass any data, the navigation controller works fine. However, when I pass data, the app crashes on runtime. What is the right way of doing this?

Here is my prepare for 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 an UIImage object. Moreover, here is the screenshot from my storyboard where I put the navigationController. I call the prepareForSeguefunction in CustomCameraViewController to segue to CropViewController.

StoryBoard Screenshot

EDIT: I have changed my prepareForSegue to 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 app does not crash but I don't know how to send an object through Navigation Controller

like image 961
Berkan Ercan Avatar asked Dec 25 '22 01:12

Berkan Ercan


1 Answers

let controller: CropViewController = segue.destinationViewController as CropViewController

Double check if segue.destinationViewController is actually the navigation view controller.

If it's the navigation controller, get CropViewController from it:

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

Note you don't have to subclass UINavigationController.

like image 152
James Chen Avatar answered Jan 19 '23 00:01

James Chen