BaseViewController子类未在swift 3中发布

时间:2017-07-13 18:10:22

标签: ios swift memory-management uiviewcontroller

我创建了class BaseViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor(red: 0.4 / 255.0, green: 100 / 215.0, blue: 120 / 255.0, alpha: 1.0) } override var preferredStatusBarStyle: UIStatusBarStyle { return preferredStatusBarStyle_Internal() } override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return supportedInterfaceOrientations_Internal() } lazy var progressHUD: MBProgressHUD = { if let navController = self.navigationController { return navController.HUD } return self.HUD }() func showAlert(_ title: String?, message: String) { let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .cancel, handler: { _ in DispatchQueue.main.async { self.progressHUD.hide(animated: false, afterDelay: 1.0) } }) alert.addAction(action) present(alert, animated: true, completion: nil) } func showPermissionDeniedAlert(_ message: String) { let alertController = UIAlertController(title: message, message: "Go to Settings?".localized, preferredStyle: .alert) let settingsAction = UIAlertAction(title: "Settings".localized, style: .default) { _ in guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { return } if UIApplication.shared.canOpenURL(settingsUrl) { if #available(iOS 10.0, *) { UIApplication.shared.open(settingsUrl, completionHandler: { success in print("Settings opened: \(success)") // Prints true }) } else { let success = UIApplication.shared.openURL(settingsUrl) print("Settings opened: \(success)") } } } alertController.addAction(settingsAction) let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil) alertController.addAction(cancelAction) present(alertController, animated: true, completion: nil) } } extension UIViewController { func preferredStatusBarStyle_Internal() -> UIStatusBarStyle { return .lightContent } func supportedInterfaceOrientations_Internal() -> UIInterfaceOrientationMask { return isiPad() ? .allButUpsideDown : .all } var HUD: MBProgressHUD { let progressHUD = MBProgressHUD(viewController: self) return progressHUD } } 作为我所有视图控制器的子类,这样我就可以在需要任何progess显示和隐藏哪个是懒惰变量时显示警告。

到目前为止,一切都很酷。但我发现我所有的视觉控制器都没有发布。有什么问题?

allprojects {
repositories {
    jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

android {
defaultConfig {
minSdkVersion 26
targetSdkVersion 26
}
productFlavors {
}
}
dependencies {
}

1 个答案:

答案 0 :(得分:0)

如果视图控制器未释放,则意味着您正在创建保留周期。

可能在两个地方

  1. 将弱自身传递给异步阻止。
  2. DispatchQueue.main.async { [weak self] in
    self?.progressHUD.hide(animated: false, afterDelay: 1.0)
    }
    
    1. 如果MBProgressHUD init正在创建保留周期,则传递弱引用
    2. var HUD: MBProgressHUD {
      // you can also use weak self
      unowned let unownedSelf = self
      
      let progressHUD = MBProgressHUD(viewController: unownedSelf)
      return progressHUD
      }