ios - 尝试关闭加载视图时应用程序崩溃
问题描述
我正在开发一个天气应用程序,每次用户的位置更新时我都会拨打网络电话,这只是每次应用程序启动时。
我还在获取信息以获得更好的用户体验时呈现加载视图,但是大约十分之一的应用程序启动时,它在尝试从超级视图中删除加载视图的背景视图时崩溃,我不知道为什么.
编辑:我已经尝试使 containerView 成为可选并且它解决了崩溃问题,但是加载视图会永远保持不变,因为在这些情况下不会关闭 containerView。
如果有人想帮助我,我会将代码附加到数据加载类和下面的调用站点。
提前感谢您的时间!
class WEDataLoadingVC: UIViewController {
var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
func presentLoadingView() {
containerView = UIView(frame: self.view.bounds)
view.addSubview(containerView)
containerView.backgroundColor = .systemBackground
containerView.alpha = 0
UIView.animate(withDuration: 0.3) { self.containerView.alpha = 0.8 }
let activityIndicator = UIActivityIndicatorView(style: .large)
containerView.addSubview(activityIndicator)
activityIndicator.centerX(in: containerView, constant: 0)
activityIndicator.centerY(in: containerView, constant: 0)
activityIndicator.startAnimating()
}
func dismissLoadingView() {
DispatchQueue.main.async {
self.containerView.removeFromSuperview()
self.containerView = nil
}
}
和呼叫站点
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return /* present alert */}
let lat = String(location.coordinate.latitude)
let lon = String(location.coordinate.longitude)
LocationManager.shared.getLocationName(for: location) { placemark in
guard let place = placemark?.locality else {
/* present alert*/
return
}
DispatchQueue.main.async { self.locationView.update(with: place) }
}
presentLoadingView()
NetworkManager.shared.getWeatherDataFor(lat: lat, lon: lon) { [weak self] result in
guard let self = self else { return }
self.dismissLoadingView()
switch result {
case .success(let data):
DispatchQueue.main.async {
self.currentConditionsView.update(with: data)
self.lookAheadView.update(with: data)
self.todayView.update(with: data)
}
case .failure(let error):
/* present alert */
print(error.rawValue)
}
}
}
解决方案
您可以尝试执行以下操作:
var activityIndicator: UIActivityIndicatorView
func presentLoadingView() {
if containerView == nil {
containerView = UIView(frame: self.view.bounds)
view.addSubview(containerView)
containerView.backgroundColor = .systemBackground
containerView.alpha = 0
UIView.animate(withDuration: 0.3) { self.containerView.alpha = 0.8 }
activityIndicator = UIActivityIndicatorView(style: .large)
containerView.addSubview(activityIndicator)
activityIndicator.centerX(in: containerView, constant: 0)
activityIndicator.centerY(in: containerView, constant: 0)
}
else {
self.containerView.isHidden = false
activityIndicator.startAnimating()
}
}
func dismissLoadingView() {
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
self.containerView.isHidden = true
}
}
推荐阅读
- php - 在其他类中使用 PDO 非常简单
- php - 会话变量未传递到另一个页面
- python - 以特殊目的更改 pandas DataFrame 中的类型?
- angular - 选择日期和时间后,p 日历弹出模式未关闭
- java - Log4j2 显示与系统时间不同的时间
- python - cv2.imshow 崩溃 - libc++abi.dylib:以 NSException 类型的未捕获异常终止
- ios - ExpandableLable 仅适用于系统字体
- dynamics-crm - 关联视图的 Dynamics CRM 分页限制因用户而异
- scala - Akka Http 测试 Gzip 响应
- delphi - Delphi Enums 到 Variant 作为 varInteger 而不是 varUInt32