William Lloyd William Lloyd - 8 days ago 7
Swift Question

Multiple PickerView on Multiple View Controller

I have one pickerview already on a viewcontroller then I have a new viewcontroller which I have tried to enter a new pickerview however everytime I test my code I get a SIGABRT error. I know it must be something to do with the fact that I already am using those functions for my first one or something but I have no idea what to do here. Any help in the right direction would be nice.

Thanks

class ViewController2: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet var pickerView2: UIPickerView!

var aparray = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"]



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView2.delegate = self
pickerView2.dataSource = self


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}




func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return aparray[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return aparray.count
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}


}

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet var pickerView: UIPickerView!

var array = ["1","2","3","4","5","6"]



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate = self
pickerView.dataSource = self


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}




func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}


}

CRASH DATA BELOW:

2016-11-29 20:12:19.441114 testapp[1950:449604] Unknown class Vc2 in Interface Builder file.


2016-11-29 20:12:19.443065 testapp[1950:449604] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key pickerView2.'
*
First throw call stack:
(0x193a7e1c0 0x1924b855c 0x193a7de88 0x194488110 0x199bda758 0x199ddc0c8 0x19399ddc4 0x199ddaa9c 0x199c3b444 0x199a0d53c 0x1998d8d48 0x1998d8c78 0x1998df424 0x1998dc8c4 0x19994f0e8 0x199b5ba78 0x199b615c8 0x199b75e60 0x199b5e5ac 0x1956258bc 0x195625728 0x195625ad0 0x193a2c278 0x193a2bbc0 0x193a297c0 0x193958048 0x1999442b0 0x19993f034 0x1000fac24 0x19293c5b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

Answer

I just made my own project and compared it to your code, and unless I'm missing some very tiny typo, you are doing everything correctly. This makes me think there is an issue with either your x code project being glitchy, or a storyboard outlet is no longer connected.

Try to fully quit Xcode and then reopen, click product and then clean.

If that doesn't work, check your storyboard outlets. When you control click on your pickerView2 in storyboard, it should show you the outlets it is connected to.

In this case, there should only be one Referencing Outlet and the circle all the way to the right of it should be filled in white(like it is in the picture below).

If it is not filled in white, delete it because it is causing the crash. Then delete the code outlet. Then recreate the outlet.

enter image description here

It looks like you are doing everything right code wise. So if it continues being awful after the clean, and the outlet is connected correctly, delete the picker and all the code that references it and just recreate it.

Let me know if all of this still doesn't work for you. If it doesn't, screenshot your storyboard/viewcontrollers in interface builder like I did.