1

I'm making an iOS app using swift3 on xcode8.

My storyboard is this ( storyboard ).

If I click the button on the third TableViewController from the left, the fourth one will appear. The fourth one is a ViewController to choose library or camera to place a photo onto the imageView on the third TableViewController.

I write the function below in the fourth ViewController's file. The function is a method called when a photo is taken or chosen.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage

    if info[UIImagePickerControllerOriginalImage] != nil {

        DispatchQueue.main.async {

            let navigationController = self.presentingViewController as! UINavigationController

            let originVc = navigationController.topViewController as! SettingTableViewController

            originVc.image = image

            self.dismiss(animated: true, completion: nil)
        }

    }

    picker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

When this is run, let navigationController = self.presentingViewController as! UINavigationController part is highlighted, and says Thread 1: signal SIGABRT. And also the error Could not cast value of type '[APPNAME].ViewController' to 'UINavigationController' (0x10f85c4a8). is shown(*[APPNAME] is my app name).

Do you know anything about this?

Thank you in advance!

Yutaro
  • 89
  • 1
  • 10
  • Because your self.presentingViewController is not navigation controller – Prashant Tukadiya Aug 30 '17 at 12:34
  • Get rid of the `as!` casts and set a debugger breakpoint that lets you see the **real** object types being assigned. It will tell you where you went wrong more quickly that a question on SO. :) – Phillip Mills Aug 30 '17 at 12:39
  • Thanks for comments! Actually, it should be navigation controller because if I make exactlly the same one in another project, it works. However, it doesn't work in my current project. By the way, the third TableViewController's type is SettingTableViewController, so I tried this "let originVc = self.presentingViewController as! SettingTableViewController" instead, but the same error still appears. Any ideas? – Yutaro Aug 31 '17 at 02:19

1 Answers1

1

This crash is telling you that your presentingViewController is not an UINavigationController, in fact is a UIViewController, so you need to use the .navigationController property and use a if let or guard to avoid any problem

DispatchQueue.main.async {

            if let navigationController = self.presentingViewController.navigationController{

               let originVc = navigationController.topViewController as! SettingTableViewController

               originVc.image = image

               self.dismiss(animated: true, completion: nil)
            }
        }
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55