在某些情况下,UIAlertController不会被驳回

时间:2019-01-31 06:28:51

标签: swift uialertcontroller

在我的项目中,将处理视频,然后在播放某些效果后将其播放。在处理视频时,我想显示Please wait...消息。

我使用UIAlertController显示警报,完成此过程后,我想隐藏警报。

如果视频处理正确,则警报将为dismissed,但是在某些情况下,该过程失败的dismiss也将不起作用!请注意,dismiss函数将被调用,但不起作用。

此外,我使用VGPlayer播放视频。

class VGViewController: UIViewController {
    var player : VGPlayer?
    var url: URL?
    var outputVideo: URL?
    override func viewDidLoad() {
        super.viewDidLoad()
        showLoading()
        DispatchQueue.global(qos: .userInitiated).async {
            do {
                guard let output = try effectVideo(fileURL: url) else {
                    DispatchQueue.main.async {
                        self.hideLoading()
                    }
                    return
                }
                outputVideo = output
            } catch let e {
                DispatchQueue.main.async {
                        self.hideLoading()
                    }
                print("video error: ",e)
            }
            self.player = VGPlayer(URL: outputVideo)
            self.player?.delegate = self
            DispatchQueue.main.async {
                self.hideLoading()
                self.view.addSubview((self.player?.displayView)!)
                self.player?.backgroundMode = .proceed
                self.player?.play()
                self.player?.displayView.delegate = self
                self.player?.displayView.titleLabel.text = self.url!.lastPathComponent
                self.player?.displayView.snp.makeConstraints { [weak self] (make) in
                    guard let strongSelf = self else { return }
                    make.edges.equalTo(strongSelf.view)
                }
            }
        }
    }
    var alert: UIAlertController!
    private func showLoading() {
        alert = UIAlertController(title: nil, message: "Please wait.\nThis may take a few seconds...", preferredStyle: .alert)
        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.style = UIActivityIndicatorView.Style.gray
        loadingIndicator.startAnimating()
        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)
    }

    private func hideLoading() {
        alert.dismiss(animated: true, completion: {print("**** alert dismiss called ****")})
    }
}

2 个答案:

答案 0 :(得分:3)

尝试添加此项以隐藏Alertview。

private func hideLoading() {
      for subview in alert.view.subviews {
          subview.removeFromSuperview()
      } 
        alert.dismiss(animated: true, completion: {print("**** alert dismiss called ****")})
}

希望这会起作用。

答案 1 :(得分:2)

是否存在错误:“警告:尝试在视图不在窗口层次结构中的<...>上显示“ UIAlertController:0x7fde1b080600”! ?

viewDidLoad中,您的应用未呈现ViewController。

尝试在viewDidAppear中执行该代码