Reputation: 101
I want to change the image depending on the variable, I'm not sure how I can do that.
I think the code I have is right, however in the iOS simulator the image just comes up as blank. I wonder why?
Here is my code below:
import UIKit
class BuyTicketViewController: UIViewController {
@IBOutlet weak var stadiumImage: UIImageView!
@IBOutlet weak var logoImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if (globalVenueSelected == "Gillingham Football Club"){
let imageGillinghmaFootballClub : UIImage = UIImage(named: "gillinghamStadium.jpg")!
let stadiumImage = (image: imageGillinghmaFootballClub)
}
if (globalVenueSelected == "Old Trafford"){
let imageOldTraffordFootballClub : UIImage = UIImage(named: "oldTraffordStadium.jpg")!
let stadiumImage = (image: imageOldTraffordFootballClub)
}
if (globalVenueSelected == "Emirates Stadium"){
let imageEmiratesFootballClub : UIImage = UIImage(named: "emiratesStadium.jpg")!
let stadiumImage = (image: imageEmiratesFootballClub)
}
if (globalVenueSelected == "Wembley"){
let imageWembley : UIImage = UIImage(named: "wemblyStadium.jpg")!
let stadiumImage = (image: imageWembley)
}
if (globalVenueSelected == "O2 Arena"){
let imageO2Arena : UIImage = UIImage(named: "o2ArenaStadium.jpg")!
let stadiumImage = (image: imageO2Arena)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Oh, and by the way, the variable globalVenueSelected is a global variable, I don't know if that affects anything but it may change the code.
Any help is appreciated, thank you.
Upvotes: 0
Views: 2856
Reputation: 5039
Try the following code.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
var imageName: String!
switch globalVenueSelected {
case "Gillingham Football Club":
imageName = "gillinghamStadium.jpg"
case "Old Trafford":
imageName = "oldTraffordStadium.jpg"
case "Emirates Stadium":
imageName = "emiratesStadium.jpg"
case "Wembley":
imageName = "wemblyStadium.jpg"
case "O2 Arena":
imageName = "o2ArenaStadium.jpg"
default: break
}
self.stadiumImage.image = UIImage(named: imageName)
}
You should put the switch statement in viewWillAppear(:)
since your IBOutlet
s may not be set in viewDidLoad
.
Also, many people consider it best practice to use a variable name that reflects the type of that variable. For example,
stadiumImage
should be renamed to stadiumImageView
.logoImage
should be renamed to logoImageView
.Properly naming your variable names will make it easier for you and others to read, understand, and maintain you code.
Furthermore, the Asset Catalog for your project will usually not accept .jpg
files by default. You can either convert your stadium images to .png
format and then add them to your Asset Catalog at design time, or you can refer to this Stack Overflow answer that explains how to make your Asset Catalog accept non-PNG assets.
Upvotes: 3