0
我已經嘗試了所有以前的建議來解決這個問題,但仍然沒有運氣。它正在工作(UIPickerView正確填充),但現在應用程序崩潰後viewDidLoad()運行。已驗證所有IBOutlet已正確連接。嘗試重新創建可可觸摸文件。Swift 3 viewDidLoad() - 致命錯誤:意外地發現零,同時展開一個可選值
這是我的視圖控制器代碼。這應該是我忽視的一些小事。幫幫我!!
class SettingsVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var targetCadenceLabel: UILabel?
@IBOutlet weak var cadenceStepper: UIStepper?
@IBOutlet weak var goalPicker: UIPickerView?
var pickerDataSource = ["improve SPM", "maintain SPM"]
override func viewDidLoad() {
super.viewDidLoad()
self.goalPicker?.dataSource = self
self.goalPicker?.delegate = self
//MARK: Setup Tab Bar
let tabBarItem = self.tabBarItem
let incomingSelectedImage: UIImage! = UIImage(named: "settings_icon")?.withRenderingMode(.alwaysOriginal)
tabBarItem?.selectedImage = incomingSelectedImage
tabBarItem?.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor(red: 60/255, green: 159/255, blue: 255/255, alpha: 1.0)], for: UIControlState.selected)
}
//MARK: - Delegates and data sources
//MARK: Data Sources
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count
}
//MARK: Delegates
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = pickerDataSource[row]
let myFont = UIFont.systemFont(ofSize: 18)
let fontColor = UIColor(red: 60/255, green: 159/255, blue: 255/255, alpha: 1.0)
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName: myFont,NSForegroundColorAttributeName:fontColor])
return myTitle
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
@IBAction func cadenceStepper(_ sender: UIStepper) {
targetCadenceLabel?.text = Int(sender.value).description
}
@IBAction func goPremiumTapped(_ sender: UIButton) {
print("Go Premium Button Tapped")
}
}
設置一個異常斷點,發現哪一行導致崩潰的 – Paulw11
請參閱http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1學習如何調試碰撞。結合重複問題,你可以找出你的問題。 – rmaddy
檢查零條件第一就是這樣,然後分配給tabbaritem圖像..... 如果讓incomingSelectedImage =的UIImage(命名爲: 「settings_icon」)?withRenderingMode(.alwaysOriginal){? tabBarItem .selectedImage = incomingSelectedImage } –