0

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?

2
  • 1
    "What is wrong in the code" You tell us. What line does the error occur on? What is the exact error message? Don't just throw the whole code at the wall like spaghetti and expect us to clean it up. Commented Jan 16, 2016 at 17:21
  • @matt, this is the complete message: Could not cast value of type 'Pedro_Villarejo_App_Clientes.CitaServicio3' (0x1032475f0) to 'Pedro_Villarejo_App_Clientes.CitaServicio4' (0x103247990), just after printing: print (segue.identifier) print (vehiculoTextField.text) Commented Jan 16, 2016 at 17:23

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

No @matt, the destination view controller is in fact CitaServicio4.
No, in fact it is not CitaServicio4. If it were CitaServicio4 you would not be crashing. Trust me on this one. Try to remember that you are the one asking the question; that means you don't know. Try to let go of your preconceptions and listen to what you're being told.
I have checked it and the segue identifier cita3a4 is right, the class is also CitaServicio4, but still crashing. I have removed CitaServicio4 ViewController and created again, and now it is working. Thank you @matt, and sorry for my bad english, I am here to learn and I appreciate when I am being told by you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.