Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在httpPost之后,PushViewController不工作_Ios_Swift3 - Fatal编程技术网

Ios 在httpPost之后,PushViewController不工作

Ios 在httpPost之后,PushViewController不工作,ios,swift3,Ios,Swift3,我制作了一个登录屏幕,它接收输入并与RESTAPI通信以验证用户。如果响应为true,则我登录用户,否则不登录。 我已经编写了一个方法openViewControllerBasedOnIdentifierid来切换视图 restapi适当地返回true和false。将调用推送控制器,但视图不会更改。如果我只在LoginAction方法“self.openViewControllerBasedOnIdentifierPlayVC”中放置一行代码,并删除其余代码,它将正常工作 这是我的密码 @iAc

我制作了一个登录屏幕,它接收输入并与RESTAPI通信以验证用户。如果响应为true,则我登录用户,否则不登录。 我已经编写了一个方法openViewControllerBasedOnIdentifierid来切换视图

restapi适当地返回true和false。将调用推送控制器,但视图不会更改。如果我只在LoginAction方法“self.openViewControllerBasedOnIdentifierPlayVC”中放置一行代码,并删除其余代码,它将正常工作

这是我的密码 @iAction func LoginAction\发送方:任何{

    //self.openViewControllerBasedOnIdentifier("PlayVC")

Constants.login_status = false
    //created NSURL
    let requestURL = NSURL(string: URL_BK)

    //creating NSMutableURLRequest
    let request = NSMutableURLRequest(url: requestURL! as URL)

    //setting the method to post
    request.httpMethod = "POST"
    let username = phonenumber.text

    //creating the post parameter by concatenating the keys and values from text field
    let postParameters = "username="+username!+"&password=bk&schoolId=0";

    //adding the parameters to request body
    request.httpBody = postParameters.data(using: String.Encoding.utf8)


    //creating a task to send the post request
    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in
        let responseData = String(data: data!, encoding: String.Encoding.utf8)
        if error != nil{
            print("error is \(error)")
            return;
        }

        //parsing the response
        do {

            print(“Received data is ---%@",responseData as Any)

            let myJSON =  try JSONSerialization.jsonObject(with: data! , options: .allowFragments) as? NSDictionary


            if let parseJSON = myJSON {


                var status : Bool!

                status = parseJSON["status"] as! Bool?
                //print(status)

                if status==false
                {
                    Constants.login_status = false

                                        }
                else{
                    Constants.login_status = true
                    print("calling PLAYVC")

                    self.openViewControllerBasedOnIdentifier("PlayVC")


                }

            }
            else{
                print("NULL VALUE RECEIVED")
            }


        } catch {
            print(error)
        }

    }
    //executing the task
    task.resume()




}

您应该在主线程上打开新的视图控制器,如下所示:

DispatchQueue.main.async {
    self.openViewControllerBasedOnIdentifier("PlayVC")
}

当您调用URLSession.shared.dataTask时,您的REST API查询响应在后台线程中处理,因此当您调用任何UI操作时,您应该如上所述包装代码,以在主线程中执行UI代码。这样就可以了:

太好了:如果有帮助,请将答案标记为正确吗?