Swift登录界面执行segue

时间:2018-02-05 06:16:45

标签: json swift uiactivityindicatorview

我使用以下代码登录用户第一个用户输入密码和id然后使用post请求我将此信息发送到服务器。

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
        let message2  = "Please wait..."
        var messageMutableString = NSMutableAttributedString()
        messageMutableString = NSMutableAttributedString(string: message2 as String, attributes: [NSFontAttributeName:UIFont(name: "HelveticaNeue-Bold",size: 15.0)!])
        alert.setValue(messageMutableString, forKey: "attributedMessage")
        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();



        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)

        if Reachability.isConnectedToNetwork() == true {

            var postString = GlobalVariable.globalIpAdress

            postString.append("&userid=")
            postString.append(userId.text!)
            postString.append("&password=")
            postString.append(password.text!)
            postString.append("&parola=")

            let urlString = postString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)
            var request = URLRequest(url: URL(string:urlString!)!)
            request.httpMethod = "POST"
            request.timeoutInterval=10
            message="Request timed out"


            let task = URLSession.shared.dataTask(with: request) { data, response, error in

                DispatchQueue.main.async {
                    self.dismiss(animated: false, completion: nil)
                }

                guard let data = data, error == nil else {
                    print("error=\(String(describing: error))")
                    DispatchQueue.main.async {
                        let alert = UIAlertView(title: "Uyarı!", message: self.message, delegate: nil, cancelButtonTitle: "OK")
                        alert.show()
                    }
                    return;
                }



                if let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? NSDictionary

                {
                    print(json)
                    if (json["error"] as? String) != nil {}

                    if let messages = json["messages"] as? NSArray{

                        for item in messages {

                            if let description = item as? AnyObject {

                                if let text = description["text"] as? String {
                                    self.message = text
                                }
                            }
                        }
                    }

                    if let tokenTemp = json["token"] as? String {

                        self.success=true
                        GlobalVariable.globalToken = tokenTemp
                    }
                }

                if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
                    print("statusCode should be 200, but is \(httpStatus.statusCode)")
                    print("response = \(String(describing: response))")
                }



                let responseString = String(data: data, encoding: .utf8)
                print("responseString = \(String(describing: responseString))")

                DispatchQueue.main.async {

                    if(self.success == true){
                        self.performSegue(withIdentifier: "secondVC", sender: self)
                    } else {
                        let alert = UIAlertView(title: "Warning!", message: self.message, delegate: nil, cancelButtonTitle: "OK")
                        alert.show()

                    }

                }
            }

            task.resume()
        }
        else{
            print("Internet connection FAILED")
            let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()

        }

首先,我正在为登录界面添加指标视图然后,一旦令牌出现,我的Bool成功变量就变为真,我正在解雇活动指示器然后它需要在下一个屏幕上消失但是它不起作用现在,我也遇到了错误。

  

警告:尝试在UIAlertController上显示UITabBarController:其视图不在窗口层次结构中!

我错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

我认为错误本身解释了这个问题:

  

尝试在UIAlertController上呈现UITabBarController:其视图不在窗口层次结构中!

当你在这行代码中展示UIAlertController时:

present(alert, animated: true, completion: nil)

但你并没有放弃它,并试图performSegue UIAlertController

self.performSegue(withIdentifier: "secondVC", sender: self)

无论何时出示UIAlertController,都会将其取消。

参考: