json - 执行 segue 的 Swift 登录屏幕

标签 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()

        }

首先,我正在为登录屏幕添加指示器 View ,然后,一旦 token 出现,我的 Bool 成功变量就会变为真,我正在关闭事件指示器,然后它需要在下一个屏幕上消失,但它不起作用现在,我也遇到了以下错误。

Warning: Attempt to present UITabBarController on UIAlertController: whose view is not in the window hierarchy!

我哪里做错了可能是什么原因?

最佳答案

我认为错误本身可以解释这里的问题:

Attempt to present UITabBarController on UIAlertController: whose view is not in the window hierarchy!

当您在这行代码中呈现 UIAlertController 时:

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

但您并没有忽略它并尝试在 UIAlertControllerperformSegue:

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

无论何时呈现 UIAlertController 都将其关闭。

引用这个:

关于json - 执行 segue 的 Swift 登录屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48616807/

相关文章:

加载 UIViewController 时的 iPhone UIActivityIndi​​catorView

ios - UIActivityIndi​​catorView 在 CollectionView 页脚中消失

java - Maven/导入基础知识 - 找不到类

json - Camel-jackson vs 2.9.0 在解码期间忽略未知属性

ios - 在 Swift 中每天刷新一次应用程序的 JSON 内容

iOS:为什么我的大文件没有用 NSData(contentsOfFile: options:) 转换?错误域=NSCocaErrorDomain 代码=256

javascript - 如何判断正在使用哪个 JSON 对象(Crockford 的或其他的)?

json - 由于信号 : Segmentation fault: 11 due to TableViewController?,命令失败

xcode - 如何初始化其他 View Controller 场景?

iphone - UIActivityIndi​​catorView 用于为 URL 加载的 UIImage