我正在为微调器和警报使用共享类。它工作正常,但我有时会遇到崩溃问题。
我在 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 用于在包括导航栏在内的所有窗口顶部显示警报和微调器。
最佳答案
如果您在 activityIndicator
之前调用 stopActivityIndicator
,则 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/