Ive created a ViewController and placed a Picker View on it - my app has three items in the array named First Second and Third, it also has a button on the ViewController to allow me to scroll to eith First Second or Third and a button below to select that item.
I have three other ViewControllers named FirstViewController, SecondViewController and ThirdViewController and I want to scroll the Picker and select one of these Three ViewControllers to show here is my code
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBAction func continueButtonPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "\(chosenState)1", sender: nil)
print("no chance")
}
@IBOutlet weak var pickerView: UIPickerView!
var pickerData = ["First","Second","Third"]
var chosenState = ""
override func viewDidLoad() {
super.viewDidLoad()
print("Help")
pickerView.delegate = self
pickerView.dataSource = self
/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
/
}
/
/
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
/
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
/
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
chosenState = pickerData[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
}
_____________________________________________________
This is my debug console output
Help
2018-03-04 09:04:27.969852+0000 Scroller[21634:3399958] Unknown class FirstViewController in Interface Builder file.
no chance
Help
2018-03-04 09:04:31.916170+0000 Scroller[21634:3399958] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<Scroller.ViewController: 0x7f9e7e416420>) has no segue with identifier 'Second1''
*** First throw call stack:
(
the annoying thing is it works sometimes
Can anyone help me work this out
Regards
Jeremy
When you post code, please use the code formatting tool (<>) that makes reading much more comfortable.
In addition, I find it better to declare IBOutlet and properties first, before the actions ; it is good also to insert MARK pragmas, for your future search in code.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
var pickerData = ["First","Second","Third"]
var chosenState = ""
// MARK: - lifecycle
override func viewDidLoad() {
super.viewDidLoad()
print("Help")
pickerView.delegate = self
pickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - IBActions
@IBAction func continueButtonPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "\(chosenState)1", sender: nil)
print("no chance")
}
// MARK: - picker delegate
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
chosenState = pickerData[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
}From your log:
1. viewDidload works OK
2. Unknown class FirstViewController in Interface Builder file.
Are you sure you have declared the destination viewController in IB as a FirstViewController ?
3. Terminating app due to uncaught exception
The segue is not found. In:
@IBAction func continueButtonPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "\(chosenState)1", sender: nil)
print("no chance")
}Are you sure you have defined a segue in IB with identifier as Second1
Note: to help debug, give more information in the print :
print("no chance with \(chosenState)1")