我有一个 FakeSplashController
,它执行一些网络请求,然后打开 WelcomeViewController
。当我查看 WelcomeViewController
中的内存图时,我看到 SplashViewController
仍然在那里。我正在调用 FakeSplashController
中的 deinit
函数来检查它是否正在 deniniting 但它没有调用它。可能是什么问题?
在 WelcomeViewController
中我在内存中看到的内容:
FakeSplashController:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupUI()
networkRequests()
}
func networkRequests(){
AppInitService().initAppRequest { [](result) in
switch result{
case .success(_):
self.startAnimation()
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}
func openApp(){
let loginController = WelcomeViewController()
self.present(loginController, animated: true)
}
func startAnimation(){
UIView.animate(withDuration: 0.8, animations: {
self.logoImage.frame.origin.x -= 100
}, completion: nil)
UIView.animate(withDuration: 1,delay: 0.3,animations: {
self.textLogo.alpha = 1
self.textLogo.frame.origin.x += 50
}, completion: { _ in
self.openApp()
})
}
deinit {
print("Splash Deinited")
}
编辑:当我在下面的帖子中用红色表示时,有人提到了这一点;
Unless you're doing something very specialized, you don't need to de-init an object in Swift. It will be called automatically when the reference count goes to 0. If you really need to, you should be able to set you window's rootViewController through your AppDelegate.
所以我不需要把这当作一个问题吗? Best way to deinit Initial view controller?
最佳答案
正如我在评论中所说,您的代码没有什么奇怪的,这就是 iOS 框架的工作方式; View Controller 没有被释放,因为它仍然是当前窗口的 rootViewController
。
我认为不需要解除分配它,但如果您确实需要这样做,解决方案可能是将 rootViewController
替换为您的 WelcomeViewController
。通过替换它,启动画面的引用计数将变为 0,并将被释放。
有点像
guard let window = UIApplication.shared.keyWindow else {
return
}
self.dismiss(animated: false, completion: nil)
window.rootViewController = WelcomeViewController()
这个的问题是没有动画(但是你也可以实现动画,只要找到how to animate rootViewController
change)
关于ios - 出现新 Controller 后 Controller 不会取消初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820169/