Imageview Swift error

Asked

Viewed 40 times

0

Fala galera...

I am developing an app, but it is with an optional error (I believe that is the error), I have the following scenario: A button "A" and a "B" in a main Viewcontroller(1) when clicking the button "A" would have to open a second Viewcontroller(2) with the image "X" and when clicking the button "B" open the same Viewcontroller(2) but loading the "Y" image. If no image, Viewcontroller(2) normally opens.

Syntax to load the image I believe to be correct:

@IBOutlet weak var fundo: UIImageView!
@IBAction func botaAcao(_ sender: Any) {
        let imagem = UIImage(named: "imagem1.jpg")!
        fundo.image = imagem

    }

because when placing an Imageview in the main Viewcontroller(1) the image loads normally, but when trying to open in the second Viewcontroller(2).

From the following error:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

1 answer

0

Speak, Rodrigo, beautiful?

Well by the time you ask the question, you’ve probably solved it.

But just to document to other devs, I’ll leave the solution here:

If the image does not only work in Viewcontroller(2), it is most likely because the "background" attribute was not linked to the element in Storyboard.

I hope I’ve helped.

  • Blz Alberto. Thanks for the comment, I already managed to resolve. Actually it was linked straight. I decided using follows, as I had several buttons and for each button a different background would open, the best was to use this way... if follows.Identifier == "boot 1"{ Let vcDestination = follows.Destination as! Newviewcontroller vcDestination.imgBotao = 1 But thank you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.