Reputation: 45
I'm trying to send an image to another view, but the problem is it doesn't display the image in the UIImage.
What I am doing is sending a String to the other view which if the string is the same as "one" or "two", etc. it is supposed to get the image from the assets.
if imagen=="one" {
miImagen == UIImage(named: "one")
}
I check what I was sending and I print it (and it works). The only thing it doesn't display it.
import UIKit
class SecondController: UIViewController {
@IBOutlet weak var miImagen: UIImageView!
var imagen = String()
override func viewDidLoad() {
super.viewDidLoad()
if imagen=="uno" {
self.miImagen.image == #imageLiteral(resourceName: "uno")
}
if imagen=="dos" {
self.miImagen.image == #imageLiteral(resourceName: "dos")
}
if imagen=="three" {
self.miImagen.image == #imageLiteral(resourceName: "three")
}
if imagen=="four" {
self.miImagen.image == #imageLiteral(resourceName: "four")
}
if imagen=="five" {
self.miImagen.image == #imageLiteral(resourceName: "five")
}
if imagen=="six" {
self.miImagen.image == #imageLiteral(resourceName: "six")
}
if imagen=="seven" {
self.miImagen.image == #imageLiteral(resourceName: "seven")
}
if imagen=="eight" {
self.miImagen.image == #imageLiteral(resourceName: "eight")
}
}
}
I added in one of my "ifs" print("Your image name is"+imagen")
and it works but it doesn't execute the other code.
Upvotes: 0
Views: 48
Reputation: 318774
You main issue is trying to use ==
instead of =
to assign the image to the image view.
After that, there are several improvements you can make. Since imagen
can only equal one value, you really should use if else
.
if imagen == "uno" {
miImagen.image = UIImage(named: "uno")
} else if imagen == "dos" {
miImagen.image = UIImage(named: "dos")
} else if ... { // etc.
}
Better yet, use a switch
:
switch imagen {
case "uno":
miImagen.image = UIImage(named: "uno")
case "dos":
miImagen.image = UIImage(named: "dos")
// and the rest
default:
break
}
But the best solution is to take into account that the image you want is the same as the value for imagen
. Replace your entire set of if
s with just:
miImagen.image = UIImage(named: imagen)
That's all you need. Just one line will work for all of your images.
Upvotes: 1