4

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

2
  • Can you share piece of your code where you share your data? I think you are trying to pass values to ui objects. Commented Dec 11, 2014 at 11:58
  • I have edited my post. Do you need anything else to understand where the problem is? Commented Dec 11, 2014 at 12:46

1 Answer 1

10
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.

2
  • It is not the navigation view controller. So that is the problem. I guess I should create a subclass of Navigation View Controller with a UIImage variable and pass the photoTaken object through navigation View controller. Is that correct? Commented Dec 11, 2014 at 18:43
  • very simple answer, glad I didn't have to subclass UINavigationController. Thank you. Commented Jun 6, 2015 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.