在异步子进程(alamofire)中传递带有segue的JSON

时间:2015-09-28 18:54:59

标签: ios swift2 alamofire

Hello stackoverflow研究员!

我试图在swift中使用mysql进行登录,到目前为止,我的应用程序从数据库接收信息,但现在,我需要它在下一个视图控制器中。据我所知,我需要覆盖prepareForSegue与下一个视图变量来接收数据和当前视图变量,事情是我不能或者没有想到,如何覆盖异步alamofire中的变量请求,还有另一种方法可以将变量作为参数进行segue,或者我应该如何处理我的alamofire中的JSON对象以将它们传递给下一个segue。

这是我的代码:

    func login(){



    let url="http://primefitness.hostoi.com/scripts/read_user_ios.php"

    let user : String = usuario.text!
    let pass : String = password.text!
    var u : Bool = false
    var p : Bool = false

    Alamofire.request(.POST ,url,parameters: ["usuario":user]).responseJSON{

        request,response,result in

        switch result{

        case .Success(let perfil):

            let json = JSON(perfil)

            if let usuarioweb : String = json["usuario"].string{
                //print(usuarioweb)
                if user == usuarioweb{
                    u = true
                }
                else{
                    u = false
                }
            }
            if let passweb = json["password"].string{
                //print(passweb)
                if pass == passweb{
                    p = true
                }
                else{
                    p = false
                }
            }

            if u && p{
                if let activo : Bool = json["activo"].boolValue{

                    if activo {
                        JLToast.makeText("Usuario Correcto", duration: JLToastShortDelay).show()
                        //self.rutina(json, user: user)
                        //print(perf)
                        let url2="http://primefitness.hostoi.com/scripts/read_rutina_ios.php"

                        Alamofire.request(.POST ,url2,parameters: ["usuario":user]).responseJSON{

                            request,response,result in

                            switch result{

                            case .Success(let rutina):

                                let json = JSON(rutina)

                                if json["sinexito"] == "No Items Found"{
                                    JLToast.makeText("No existe rutina.", duration: JLToastShortDelay).show()
                                }
                                else{
                                    self.cargarNewView(JSON(perfil), rutina: json)
                                }


                            case .Failure(let data, let error):

                                JLToast.makeText("Error con la base de datos, intentelo de nuevo.", duration: JLToastShortDelay).show()
                                print("Fallido con error: \(error)")

                                if let data=data{

                                    print("Datos respondidos: \(NSString(data: data,encoding: NSUTF8StringEncoding))")

                                }

                            }

                        }


                    }
                    else{
                        JLToast.makeText("Usuario Inactivo, contacte a su administrador", duration: JLToastShortDelay).show()
                    }
                }

            }
            else{
                JLToast.makeText("Usuario/Contraseña Incorrecto", duration: JLToastShortDelay).show()
            }



        case .Failure(let data, let error):

            JLToast.makeText("Error con la base de datos, intentelo de nuevo.", duration: JLToastShortDelay).show()
            print("Fallido con error: \(error)")

            if let data=data{

                print("Datos respondidos: \(NSString(data: data,encoding: NSUTF8StringEncoding))")

            }

        }

    }

}//final login    

func cargarNewView(perfil: JSON,rutina: JSON){

    print(perfil)
    print(rutina)
    performSegueWithIdentifier("login", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if segue.identifier == "login"{
        let destinationVC = segue.destinationViewController as! rutinaViewController
        destinationVC.prueba = "hola"//testing the override
    }

    // Create a new variable to store the instance of the next view controller

}

编辑:

得到了Paulw11的答案,我正在为将来的参考添加正确的代码:

添加实例变量:

var perfil:JSON?
var rutina:JSON?

将值分配给接收请求值的函数中的实例变量:

    func cargarNewView(per: JSON,rut: JSON){

    perfil = per
    rutina = rut

    performSegueWithIdentifier("login", sender: self)
}

覆盖prepareForSegue如下:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if segue.identifier == "login"{
        let destinationVC = segue.destinationViewController as! rutinaViewController
        destinationVC.perfil = perfil
        destinationVC.rutina = rutina


    }

    // Create a new variable to store the instance of the next view controller

}

0 个答案:

没有答案