Reputation: 5101
I am implementing an UIPickerView
populated from parse.com objects.
I want to show the selected row string from the pickerView
in a TextField
.
At execution, when the user selects a row the TextField
shows the text but then when trying to pass the TextField
text to another ViewController
, the app launches an exception:
Could not cast value of type
This is the code:
class CitaServicio3: UIViewController,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate {
var receivedNombre: String = ""
var receivedEmail: String = ""
var receivedCelular: String = ""
var receivedTelefono: String = ""
var receivedFecha: String = ""
var receivedHora: String = ""
@IBOutlet weak var vehiculoPickerView: UIPickerView!
@IBOutlet var vehiculoTextField:UITextField!
var pickerString:NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
print("receivedNombre=",receivedNombre)
print("receivedEmail=",receivedEmail)
print("receivedCelular=",receivedCelular)
print("receivedTelefono=",receivedTelefono)
print("receivedFecha=",receivedFecha)
print("receivedHora=",receivedHora)
let query = PFQuery(className: "autos")
query.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects! {
print (object["modelo"])
self.pickerString.addObject(object["modelo"] as! String)
}
}
self.vehiculoPickerView.reloadAllComponents()
})
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return self.pickerString.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return self.pickerString[row] as? String
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
self.vehiculoTextField.text = self.pickerString[row] as? String
}
@IBAction func continuarButton(sender: AnyObject)
{
let vehiculoCita = vehiculoTextField.text
if (vehiculoCita!.isEmpty ){
let myAlert = UIAlertController(title: "Faltan datos", message: "Su vehiculo es obligatorio", preferredStyle: UIAlertControllerStyle.Alert)
let OKAlert = UIAlertAction(title: "Reintentar", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(OKAlert)
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
[self .performSegueWithIdentifier("cita3a4", sender: self)]
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
print (segue.identifier)
print (vehiculoTextField.text)
if "cita3a4" == segue.identifier {
let cita4: CitaServicio4 = segue.destinationViewController as! CitaServicio4
cita4.receivedNombre = receivedNombre
cita4.receivedEmail = receivedEmail
cita4.receivedCelular = receivedCelular
cita4.receivedTelefono = receivedTelefono
cita4.receivedFecha = receivedFecha
cita4.receivedVehiculo = vehiculoTextField.text!
}
}
What is wrong in the code?
Upvotes: 0
Views: 401
Reputation: 535915
You are saying:
let cita4: CitaServicio4 = segue.destinationViewController as! CitaServicio4
But the destination view controller of this segue is not a CitaServicio4. (It is, in fact, a CitaServicio3.) Therefore you crash at runtime when the forced cast turns out to be impossible.
It seems that the structure of things in your storyboard is not what you think it is. Recheck your segues, esp. their storyboard identifiers and the class of the view controller to which they lead.
Upvotes: 2