【问题标题】:BaseViewController subclasses are not releasing in swift 3BaseViewController 子类未在 swift 3 中发布
【发布时间】:2017-12-18 16:18:27
【问题描述】:

我创建了BaseViewController 用作我所有视图控制器的子类,这样当我需要任何进度来显示和隐藏哪个是惰性变量时,我就可以显示警报。

到目前为止,一切都很酷。但我发现我所有继承自此的视图控制器都没有释放。有什么问题?

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

【问题讨论】:

    标签: ios swift memory-management uiviewcontroller


    【解决方案1】:

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

    可能在两个地方

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多