您好我正在尝试一个简单的注册应用程序,当完成后,转到主页视图,但当我尝试去HomeView我的应用程序崩溃
这是代码
@IBAction func Registrar(sender: AnyObject) {
let userName = UserName.text!
let userEmail = UserEmail.text!
let medicRegistry = MedicRegistry.text!
let specialty = Speciality.text!
let country = Country.text!
let city = Ciudad.text!
let userPass = UserPassword.text!
let repeatPass = RepeatPassword.text!
if ( userName.isEmpty || userEmail.isEmpty || medicRegistry.isEmpty || speciality.isEmpty || country.isEmpty || city.isEmpty || userPass.isEmpty || userEmail.isEmpty ) {
let alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Por favor llene todos los campos"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
} else if ( userPass != repeatPass) {
let alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Passwords doesn't Match"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
//NSUserDefaults.standarUserDefaults().setObject(id, forKey:"userId")
//NSUserDefaults.standarUserDefaults().synchronize()
}else if check == false{
let alertView:UIAlertView = UIAlertView()
alertView.title = "Fallo al Registrar!"
alertView.message = "Debes aceptar los terminos y condiciones"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
else {
let url_servicio = "http://mydirection.php"
let request = NSMutableURLRequest(URL: NSURL(string: url_servicio)!)
request.HTTPMethod = "POST"
let postString = "nombre=\(userName)&email=\(userEmail)®istro=\(medicRegistry)&esp=\(specialty)&pais=\(country)&ciudad=\(city)&contrasena=\(userPass)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error -> Void in
if (error != nil){
print(error!.localizedDescription)
}else{
let nsdata:NSData = NSData(data: data!)
do{
let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: NSJSONReadingOptions.MutableContainers)
let Id = json["id"] as! String
let c: String = ""
if (Id != c){
print (Id)
self.performSegueWithIdentifier("funciona", sender: self)
}
}
catch{
print("Error del JSON")
}
}
}
task.resume()
}
}
}
答案 0 :(得分:0)
答案在错误消息中:“仅在主线程上运行”。
您的完成处理程序在后台运行,但正尝试使用segue更改UI。您应该将performSegueWithIdentifier
发送回主线程。
答案 1 :(得分:0)
我建议使用Alamofire
。函数dataTaskWithRequest
似乎没有在主线程中运行并导致冲突。
如果您不想更改Alamofire
。所以使用这个:
dispatch_async(dispatch_get_main_queue(),{
//here your segue
})