0

I have faced a problem with setting uiimageview from other viewcontroller.

DiaryDetailController.swift

let imageViewPageController = (segue.destinationViewController as! ImageViewPageController)
        if (imageCount > 0) {
            imageViewPageController.image1 = tempImage1
        }

ImageViewPageController.swift

let page: ImageViewDetailController! = storyboard?.instantiateViewControllerWithIdentifier("imageViewDetailController") as! ImageViewDetailController
            page.setImage(image1)
            pages.append(page)

ImageViewDetailController.swift

func setImage(image: UIImage) {
    imageView.image = image
}

And I got following error on "imageView.image = image". fatal error: unexpectedly found nil while unwrapping an Optional value

How should I solve this problem?

  • The code above doesn't show where imageView is declared and instantiated. If you put a breakpoint on `imageView.image = image` and see if imageView is not nil. Or if it was declared as an optional originally, then the line should read `imageView?.image = image` – DJohnson Jun 05 '16 at 20:47
  • Do your research before asking questions: http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu Your IBOutlet is probably disconnected. – Pranav Wadhwa Jun 06 '16 at 01:25
  • Thank for your comments. I've solved the problem. It was my mistake forgot linking the IBOutlet... – Young Jun Choi Jun 06 '16 at 13:01

1 Answers1

1

You're probably trying to set the image before the IBOutlets are set.

Create a UIImage property in ImageViewDetailController (let's call it image), and instead of saying:

page.setImage(image1)

You can do this

page.image = image1

Then let the ImageViewDetailController deal with it once it has loaded. So in the viewDidLoad, just say

imageView.image = self.image
R Moyer
  • 1,683
  • 1
  • 10
  • 10