首页 > 解决方案 > 尝试关闭加载视图时应用程序崩溃

问题描述

我正在开发一个天气应用程序,每次用户的位置更新时我都会拨打网络电话,这只是每次应用程序启动时。

我还在获取信息以获得更好的用户体验时呈现加载视图,但是大约十分之一的应用程序启动时,它在尝试从超级视图中删除加载视图的背景视图时崩溃,我不知道为什么.

编辑:我已经尝试使 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)
        }
    }
}

标签: iosswift

解决方案


您可以尝试执行以下操作:

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
   }
}

推荐阅读