ios - 如何在同一类中使用警报和微调器?

标签 ios swift design-patterns uialertcontroller uiactivityindicatorview

我正在为微调器和警报使用共享类。它工作正常,但我有时会遇到崩溃问题。

我在 SharedClass 中的代码

import UIKit

class SharedClass: NSObject {

static let sharedInstance = SharedClass()

var transparentView:UIView!
var spinner = UIActivityIndicatorView()

//Show activity indicator
func activityIndicator(view:UIView) {

    DispatchQueue.main.async {
    //            if let window = UIApplication.shared.keyWindow {//Conditionally unwrap it instead of force unwrap

            //let window = UIApplication.shared.keyWindow! //Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
            self.transparentView = UIView()
            self.transparentView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
            self.transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
            view.addSubview(self.transparentView)

            if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
                self.spinner = UIActivityIndicatorView(style: .whiteLarge)
                self.spinner.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
            } else {
                self.spinner = UIActivityIndicatorView(style: .white)
                self.spinner.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            }
            self.spinner.center = view.center
            self.transparentView.addSubview(self.spinner)
            self.spinner.startAnimating()

            DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {//Stop spinner after 10 Sec's
                self.stopActivityIndicator()
            }
        }
//        }
}

//Stop activity indicator
func stopActivityIndicator() {
    DispatchQueue.main.async {
        self.spinner.stopAnimating()
        self.spinner.removeFromSuperview()
        self.transparentView.removeFromSuperview()//Some times getting error here
    }
}

//Present alert on top of all windows
func alertWindow(title: String, message: String) {
    //Calling
    //SharedClass.sharedInstance.alertWindow(title:"", message:"")
    DispatchQueue.main.async(execute: {
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIViewController()
        window.windowLevel = UIWindow.Level.alert + 1

        let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in
        })
        alert2.addAction(defaultAction2)

        window.makeKeyAndVisible()

        window.rootViewController?.present(alert2, animated: true, completion: nil)
    })
}

private override init() {

}

}

有时我在这一行中遇到错误

let window = UIApplication.shared.keyWindow!//Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

self.transparentView.removeFromSuperview()//Some times getting error here

如何在同一类中正确使用这两个警报和微调器。

哪个好用

在共享类中编写代码或在个人类中编写代码。

这里我写了 window 用于在包括导航栏在内的所有窗口顶部显示警报和微调器。

最佳答案

如果您在 activityIndi​​cator 之前调用 stopActivityIndi​​cator,则 transparentView 将为 nil。所以不要使用隐式解包可选。

改变

var transparentView:UIView!

var transparentView:UIView?

并在使用这个变量时使用可选链

self.transparentView?.removeFromSuperview()

并使用guard let获取key window

guard let window = UIApplication.shared.keyWindow else {
    return
}

警报的 UIViewController 扩展

extension UIViewController {
    func showAlert(title: String, msg: String) {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

您可以像这样从任何 View Controller 调用此方法。它将出现在所有 View 和导航栏的顶部。

self.showAlert("Alert", msg: "Alert message")

关于ios - 如何在同一类中使用警报和微调器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55508549/

相关文章:

objective-c - 在 ios 中订购容器?

ios - 使用类别位掩码无法阻止物理场与粒子系统相互作用

ios - 如何在 Swift 中记录完整的 POST 请求

swift - 如何使用 NSTimer 从 10 秒开始倒计时然后停止计时器?

Javascript - 想知道这个单例是否可以使用

javascript - 将异步调用转换为同步?

c# - View 模型应该更接近 View 还是模型?

非全屏模式下的 iOS 5 后置摄像头预览

ios - 如何防止异步方法表单被调用两次?

ios - 即使不再显示,SwiftUI View 也会不断更新