Search code examples
iosswiftuibuttonuiimageoptional-values

Set UIButton Image in Swift - currently unwraps as nil


I am having a difficult time setting an image of a UIButton. I have checked other posts on how to do this, and it seems straightforward enough (but then, here I am).

In my program, the image that the button displays depends on trip.weather. I am using a switch to set the image before trying to set the button's image. However, while 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 unwrapping an 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()


}

Solution

  • Try this after NSLog(" (theImage.description)")

        if let tempButton = self.weatherButtonImage
        {
        tempButton.setImage(theImage, forState:.Normal)
        } else
        { NSLog("Button is nil")
        }
    

    To determine if its your button that is nil.

    If your button is nil, reconnect it in storyboard.