UILabel返回零

时间:2015-09-04 02:47:29

标签: ios xcode swift

src

我正在尝试创建3个pickerViews来选择一年,一个月和一天。并且有一个标签import UIKit class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { var pickerViewYears: UIPickerView! var pickerViewMonths: UIPickerView! var pickerViewDays: UIPickerView! var showDateLabel: UILabel! var years = [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020] var months = [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12] var days = [01 ,02 ,03 ,04 ,05, 06, 07, 08, 09, 10 ,11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] override func viewDidLoad() { super.viewDidLoad() pickerViewYearsSetting() pickerViewMonthsSetting() pickerViewDaysSetting() showDateLabelSetting() } func pickerViewYearsSetting() { pickerViewYears = UIPickerView() pickerViewYears.frame = CGRectMake(0, 0, 100, 100) pickerViewYears.center.x = view.center.x - 100 pickerViewYears.center.y = view.center.y view.addSubview(pickerViewYears) //pickerViewYears.layer.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 0.5).CGColor pickerViewYears.dataSource = self pickerViewYears.delegate = self } func pickerViewMonthsSetting() { pickerViewMonths = UIPickerView() pickerViewMonths.frame = CGRectMake(0, 0, 100, 100) pickerViewMonths.center = view.center view.addSubview(pickerViewMonths) //pickerViewMonths.layer.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 0.5).CGColor pickerViewMonths.dataSource = self pickerViewMonths.delegate = self } func pickerViewDaysSetting() { pickerViewDays = UIPickerView() pickerViewDays.frame = CGRectMake(0, 0, 100, 100) pickerViewDays.center.x = view.center.x + 100 pickerViewDays.center.y = view.center.y view.addSubview(pickerViewDays) //pickerViewMonths.layer.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 0.5).CGColor pickerViewDays.dataSource = self pickerViewDays.delegate = self } // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 1 } // returns the # of rows in each component.. func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if pickerView == pickerViewYears { return years.count } else if pickerView == pickerViewMonths { return months.count } else { return days.count } } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { if pickerView == pickerViewYears { return "\(years[row])" } else if pickerView == pickerViewMonths { return "\(months[row])" } else { return "\(days[row])" } } func showDateLabelSetting() { let selectedRowYears = pickerViewYears.selectedRowInComponent(0) let selectedRowMonths = pickerViewMonths.selectedRowInComponent(0) let selectedRowDays = pickerViewDays.selectedRowInComponent(0) showDateLabel.frame = CGRectMake(0, 0, 150, 100) showDateLabel.center.y = view.center.y + 150 showDateLabel.center.x = view.center.x showDateLabel?.text = "\(years[selectedRowYears]) \(months[selectedRowMonths]) \(days[selectedRowDays])" view.addSubview(showDateLabel) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 应该显示结果(如2019 4 21)。

运行模拟器时,

showDateLabel

始终显示在thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0) 部分。我已经尝试运行没有标签部分的模拟器,它可以正常运行。它也说

func showDateLabelSetting()

在调试区域只是因为我不知道该怎么办。

这是调试区域中的内容(我仍然不确定哪个部分是有用的信息,所以我在这里复制粘贴)

左侧部分

fatal error: unexpectedly found nil while unwrapping an Optional value

正确的部分

    self    
    PickerViewPractice001.ViewController    0x00007fdcabc250e0  
    UIKit.UIViewController  
    UIViewController        
    pickerViewYears UIPickerView!   0x00007fdcabd42300
    pickerViewMonths    UIPickerView!   0x00007fdcabd3f460
    pickerViewDays  UIPickerView!   0x00007fdcabd3f8c0
    showDateLabel   UILabel!    nil None
    years   [Int]   20 values   
    months  [Int]   12 values   
    days    [Int]   31 values   
    selectedRowYears    Int 0
    selectedRowMonths   Int 0
    selectedRowDays Int 0

谁能说出这里的问题是什么以及如何解决?感谢

1 个答案:

答案 0 :(得分:0)

showDateLabel尚未初始化。试试这个:

func showDateLabelSetting() {
    let selectedRowYears = pickerViewYears.selectedRowInComponent(0)
    let selectedRowMonths = pickerViewMonths.selectedRowInComponent(0)
    let selectedRowDays = pickerViewDays.selectedRowInComponent(0)
    showDateLabel = UILabel() // Added this line
    showDateLabel.frame = CGRectMake(0, 0, 150, 100)
    showDateLabel.center.y = view.center.y + 150
    showDateLabel.center.x = view.center.x
    showDateLabel?.text = "\(years[selectedRowYears]) \(months[selectedRowMonths]) \(days[selectedRowDays])"

    view.addSubview(showDateLabel)
}