【问题标题】:Set UIButton Image in Swift - currently unwraps as nil在 Swift 中设置 UIButton 图像 - 当前解包为 nil
【发布时间】:2015-05-03 20:49:56
【问题描述】:

我很难设置 UIButton 的图像。我已经查看了有关如何执行此操作的其他帖子,这似乎很简单(但是,我在这里)。

在我的程序中,按钮显示的图像取决于trip.weather。在尝试设置按钮的图像之前,我正在使用开关来设置图像。但是,在调试时,控制台会显示以下内容:

'2015-05-03 13:40:31.117 PackPlanner[581:4465] , {600, 300} 致命错误:在展开可选值时意外发现 nil (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()


}

【问题讨论】:

  • 它停在哪一行?哪个对象是 nil?
  • 这里:self.weatherButtonImage.setImage(theImage, forState: .Normal)。 UIImage 本身似乎很好。我刚刚编辑了问题以显示错误之前的 NSLog 输出。
  • 所以要么theImage 为零,要么weatherButtonImage 为零。它是哪一个?您是否仔细检查了图像名称?
  • weatherButtonImage 为零。我已经检查了很多次图像名称,这让我发疯了! :(
  • 转换为 UIImage!没有意义

标签: ios swift uibutton uiimage optional-values


【解决方案1】:

您确定 theImage 存在且不为零吗? UIImage 可能无法找到名为“Sunny”的图像。另一种可能性是self.weatherButtonImage在那个时候是nil(我看到它是一个插座),所以当IB布局在viewDidLayoutSubviews完成时尝试设置它。

【讨论】:

  • 这应该是一条评论。
【解决方案2】:

在 NSLog(" (theImage.description)") 之后试试这个

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

确定您的按钮是否为 nil。

如果您的按钮为零,请在情节提要中重新连接它。

【讨论】:

  • 查看此链接了解如何重新连接您的 IBOutlet stackoverflow.com/questions/24523086/…
  • 谢谢。我刚刚尝试过这样的事情,而 Button 绝对是零。我怀疑我在故事板文件中一定做了一些奇怪的事情。我重新连接了它。没有运气。
  • 您可能在之前的尝试中有错误的链接。转到您的故事板,在您的视图控制器顶部,右键单击带有正方形的黄色圆圈。如果您看到任何带有黄色三角形的连接,请将它们删除,因为它们是错误的链接。我必须一直这样做。
  • 我看到你在设置tripItem时运行configureView。是否可以在加载视图之前设置tripItem?如果是这样,这对您的按钮来说是个坏消息。
  • ConfigureView 应该只确定图像而不是将其放置在按钮上,因为 configureView 可以在视图加载和按钮创建之前运行...
猜你喜欢
  • 2017-03-14
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2010-11-30
  • 2015-02-27
相关资源
最近更新 更多