在我的项目中,一个视频将被处理,然后经过一些效果,它将被播放。处理视频时,我希望显示Please wait...消息。
我使用UIAlertController显示警报,完成该过程后,我希望隐藏警报。
如果视频处理正确,警报将为dismissed,但在某些情况下,处理失败也将不起作用。请注意,将调用dismiss函数,但它不起作用。
此外,我还使用dismiss播放视频。

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 ****")})
    }
}

最佳答案

尝试添加此项以隐藏AlertView。

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

希望这能奏效。

关于swift - 在某些情况下,UIAlertController不会被解雇,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54454587/

10-10 20:43