gpt4 book ai didi

ios - 带有 Activity Loader 的 Alert ViewController 收到警告而不是关闭 Swift

转载 作者:行者123 更新时间:2023-11-28 07:27:05 25 4
gpt4 key购买 nike

在我的场景中,我试图在 AlertViewController 中创建 Loader。在这里,我得到低于警告并且在两次试验后不允许解雇。我在公共(public)类中使用以下函数并在多个 viewController 中重用。

我的代码

    // MARK: Common AlertView
extension UIViewController {

func loadinHubShow() {

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
}

func loadinHubDismiss() {
dismiss(animated: false, completion: nil)
}
}

其他 View Controller

    func dataJson() {

// Start Loading
self.loadinHubShow()

// after process done
DispatchQueue.main.async {
self.loadinHubDismiss()
}
}

我的警告

Warning: Attempt to dismiss from view controller while a presentation or dismiss is in progress!

最佳答案

如我所见,您将此函数用作 UIViewController 扩展。

获得结果的一种方法是获取对您正在使用的警报的引用。

NOTE: If you use dismiss function as you did, you're trying to dismiss the viewController, not the alert, that's why you're getting that warning.

尝试以这种方式更改您的扩展功能:

1) loadinHubShow 将返回一个 referencealert

      func loadinHubShow() -> UIAlertController {

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)

return alert
//You don't have to present the alert here
//present(alert, animated: true, completion: nil)
}

2) loadinHubDismiss 将删除该警报:

         func loadinHubDismiss(alert: UIAlertController) {
alert.dismiss(animated: false, completion: nil)
}

为了使用这些功能,我们假设您有 ViewController:

            class ViewController: UIViewController{

var myAlert: UIAlertController = UIAlertController()

override func viewDidLoad(...){
myAlert = self.loadinHubShow()
//now you can present or dismiss the alert wherever you want
//for example:
self.present(myAlert,animated: false, completion: nil)

//when you want dismiss the alert, just call:
self. loadinHubDismiss(alert: myAlert)
}



}

编辑

要按照建议关闭警报,请尝试:

   DispatchQueue.main.async{
loadinHubDismiss(alert: myAlert)
}

关于ios - 带有 Activity Loader 的 Alert ViewController 收到警告而不是关闭 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com