0

I've tried all the previous suggestions here for fixing this problem, but still have had no luck. It was working (UIPickerView was correctly populating), but now the app crashes after viewDidLoad() runs. Verified all IBOutlets are correctly connected. Tried re-creating the cocoa touch file.

Here is my view controller code. It's gotta be something minor I'm overlooking. Help!!

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")
}    
}
  • Set an exception breakpoint and find which line it is crashing on – Paulw11 Sep 28 '16 at 02:20
  • Please see http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 to learn how to debug a crash. Combined with the duplicate question you can figure out your issue. – rmaddy Sep 28 '16 at 03:31
  • check nil condition first like this , then assign to tabbaritem image..... if let incomingSelectedImage = UIImage(named: "settings_icon")?.withRenderingMode(.alwaysOriginal){ tabBarItem?.selectedImage = incomingSelectedImage } – Venkat Reddy Sep 28 '16 at 14:02

1 Answers1

2

You are unwrapping in only one line in your viewDidLoad and thats here:

let incomingSelectedImage: UIImage! = UIImage(named: "settings_icon")?.withRenderingMode(.alwaysOriginal)

After verifying with the exception breakpoint that this is the line the code is crashing, you might want to do

 let incomingSelectedImage:UIImage? ..........................
Abhishek Arora
  • 395
  • 2
  • 12