我很难设置 UIButton 的图像。我已经检查了有关如何执行此操作的其他帖子,这似乎很简单(但是,我在这里)。
在我的程序中,按钮显示的图像取决于trip.weather。在尝试设置按钮的图像之前,我正在使用开关来设置图像。但是,在调试时,控制台会显示以下内容:
'2015-05-03 13:40:31.117 PackPlanner[581:4465] < UIImage: 0x7fd2a843fac0>, {600, 300} 致命错误:在展开可选值 (lldb) 时意外发现 nil'
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()
}