我正在实现一个从parse.com对象填充的UIPickerView
。
我想在apickerView
中显示从TextField
中选择的行字符串。
在执行时,当用户选择一行时,TextField
会显示文本,但当试图将TextField
文本传递给另一行时,应用程序会启动一个异常:
无法强制转换类型的值
这是代码:
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!
}
}
密码有什么问题?
最佳答案
你是说:
let cita4: CitaServicio4 = segue.destinationViewController as! CitaServicio4
但是这个segue的目的地视图控制器不是CitaServicio4。(事实上,这是一个CitaServicio3。)因此,当强制强制转换不可能时,您会在运行时崩溃。
似乎你故事板里的东西结构并不是你想象的那样。重新检查您的segue,特别是它们的脚本标识符和它们指向的视图控制器的类。
关于ios - 无法在UIPickerView中强制转换类型异常的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34829794/