【问题标题】:completion handler can not dismiss the popAlertController完成处理程序无法关闭 popAlertController
【发布时间】:2018-07-31 07:20:33
【问题描述】:

我有一个带有单个 textView 的视图控制器,我从服务器获取一个字符串并在其上设置。

我在 viewDidAppear 中调用了一个 http-get 服务,并且我需要为等待用户显示一个弹出警报,直到获取字符串

我做了所有,但我无法在加载警报时关闭 popalert 和应用程序堆栈

    override func viewWillAppear(_ animated: Bool) {
        fetchAndPrintEachPerson()

        networkReachablity()

        popUpLoading() // start showing loading pop alert

        getPerson ()

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        style()
        giftTextFiled.isEnabled = false



    }

    func popUpLoading(){

        popUpAlert = UIAlertController(title: nil, message: "wait ...", preferredStyle: .alert)

        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();

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


    }

    func getPerson(){

        guard let url=URL(string: "\(address)person/code") else {return}

        print(url)

        URLSession.shared.dataTask(with: url) { (data, response, error) in

            if let res=response {

                //print(res)
            }
            do {

                if let dataContent=data {

                    let con = try? JSONSerialization.jsonObject(with: dataContent, options: []) as? AnyObject
                    let data = con??["data"] as? AnyObject
                    let code = data?["code"] as? String
                    print(code)
                    // self.popUpAlert.dismiss(animated: true, completion: {
                     DispatchQueue.main.async(execute: {
                    //self.giftTextFiled.text=code!
                    self.giftTextFiled.isEnabled = true
                self.popUpAlert.dismiss(animated: true, completion: nil)

                     })
                    //})
                }else {
                    let snack=snackBarAlert()
                    snack.alert(title: "error", color: UIColor.red)
                    self.popUpAlert.dismiss(animated: true, completion: nil)
                }
            }catch let err{
                print(err)
DispatchQueue.main.async(execute: {
                self.popUpAlert.dismiss(animated: true, completion: nil)

})
            }
        }.resume()
    }

更新第一篇文章。

【问题讨论】:

  • 控制台中有错误信息吗?因为这和UI有关,所以它应该在主线程中。

标签: swift xcode uialertcontroller http-get completion


【解决方案1】:

尝试将该视图放入 DidLoad 并检查您是否从服务器获得响应,否则它将通过 else 块并继续显示弹出窗口

【讨论】:

  • 我已经处理了这两种情况,但有些时候没有关闭弹出窗口
【解决方案2】:

URLSession.shared.dataTask 完成闭包在非主线程上调用,可能会导致 UI 出现意外结果。将您的 UI 代码分派到主线程。

URLSession.shared.dataTask(with: url) { (data, response, error) in
    DispatchQueue.main.async {
        // Your UI related code
    }
}.resume()

【讨论】:

  • 我在我的代码中评论了 DispatchQueue 这意味着我之前已经测试过它。一段时间不关闭弹出窗口并继续显示它。
  • 在您的代码中 dismiss 不会在调度块内调用。我只能看到您正在设置giftTextFiled。您还在非主线程的 else 子句中调用 dismiss。并且您的 getPerson 完成在非主线程上调用。
猜你喜欢
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
相关资源
最近更新 更多