Set UIButton Image to Swift - currently expands to zero
I'm having a hard time setting the UIButton image. I've checked other posts on how to do this and it seems simple enough (but then, I'm here).
In my program, the image displayed on the button depends on trip.weather. I am using a switch to set the image before trying to set the button image. However, when debugging, the console displays the following:
'2015-05-03 13: 40: 31.117 PackPlanner [581: 4465] <UIImage: 0x7fd2a843fac0>, {600, 300} fatal error: unexpectedly found nil while expanding optional value (Lldb)'
class TripOverviewViewController: UITableViewController {
@IBOutlet var highTempLabel: UILabel!
@IBOutlet var lowTempLabel: UILabel!
@IBOutlet var weatherLabel: UILabel!
@IBOutlet var locationLabel: UILabel!
@IBOutlet var dateLabel: UILabel!
@IBOutlet var numOfPeopleLabel: UILabel!
@IBOutlet var sunriseLabel: UILabel!
@IBOutlet var sunsetLabel: UILabel!
@IBOutlet weak var weatherButtonImage: UIButton!
@IBAction func weatherButton(sender: UIButton) {
}
var tripItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the trip detail.
if let trip: Trip = self.tripItem as? Trip{
if var label = self.locationLabel {
label.text = trip.location
}
if var label = self.numOfPeopleLabel {
label.text = trip.numOfPeople.stringValue
}
if var label = self.dateLabel {
var formattedDate = NSDateFormatter.localizedStringFromDate(trip.startDate, dateStyle: .LongStyle, timeStyle: .NoStyle)
label.text = formattedDate
}
if var label = self.weatherLabel {
label.text = trip.weather
}
var theImage = UIImage(named: "Sunny") as UIImage!
switch trip.weather {
case "Partly Cloudy":
theImage = UIImage(named:"PartlyCloudy")
case "Light Snow":
theImage = UIImage(named:"LightSnow")
case "Rainy":
theImage = UIImage(named:"Rainy")
default:
theImage = UIImage(named:"Sunny")
}
NSLog(" \(theImage.description)") //for debugging
self.weatherButtonImage.setImage(theImage, forState: .Normal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.configureView()
}
+3
source to share
2 answers