我有一个FakeSplashController;
有些东西会阻止这个ViewController被释放,并且deinit函数不会被调用。
此外,AppInitService有一个静态函数,它在这个SplashController中被调用。我还尝试将[weak self]添加到网络请求中。然而,这也解决不了问题。
class SplashViewController: UIViewController {
let logoImage: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "logo")
imageView.contentMode = .scaleAspectFit
return imageView
}()
let textLogo: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "text-logo")
imageView.contentMode = .scaleAspectFit
return imageView
}()
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()
guard let window = UIApplication.shared.keyWindow else {
return
}
window.rootViewController = loginController
}
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")
}
func setupUI(){
self.view.backgroundColor = Color.NavigationBar.tintColor
logoImage.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
logoImage.center = self.view.center
self.view.addSubview(logoImage)
textLogo.frame = CGRect(x: 0,y: 0, width: 195, height: 80)
textLogo.center = self.view.center
textLogo.frame.origin.x -= 20
self.view.addSubview(textLogo)
textLogo.alpha = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}发布于 2018-06-25 11:13:05
你在这个街区捕捉self ..。
func networkRequests(){
AppInitService().initAppRequest { result in
switch result{
case .success(_):
self.startAnimation() // HERE BE DRAGONS!
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}这是你记忆泄漏的潜在原因。
你可以通过更新到.
func networkRequests(){
AppInitService().initAppRequest { [weak self] (result) in
switch result{
case .success(_):
self?.startAnimation()
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}发布于 2018-06-25 11:10:04
您可以首先获取数组中的所有视图控制器,然后在检查相应的视图控制器类后,可以删除所需的视图控制器。
下面是一小部分代码:
var navArray:Array = (self.navigationController?.viewControllers)!
navArray.removeAtIndex(0)
self.navigationController?.viewControllers = navArray我想这会让你的工作更轻松。
发布于 2018-06-25 11:12:14
问题是,我创建这个SplashController作为一个全局的和可选的变量在AppDelegate中;
var splashController: SplashController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
splashController = SplashController()
self.window?.rootViewController = splashController
return true
}我把它写成:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let splashController = SplasController()
self.window?.rootViewController = splashController
return true
}https://stackoverflow.com/questions/51021968
复制相似问题