gpt4 book ai didi

ios - 调用 applicationWillEnterForeground 后重新启动应用程序

转载 作者:行者123 更新时间:2023-11-30 11:09:33 24 4
gpt4 key购买 nike

我想从第一个 View 重新启动应用程序,如下所示:

func applicationWillEnterForeground(_ application: UIApplication) {
let storyBoard: UIStoryboard = UIStoryboard(name: "SplashScreen", bundle: nil)
let splashScreen = storyBoard.instantiateViewController(withIdentifier: "splashVC") as! SplashScreenController
self.window?.addSubview(splashScreen.view)

}

但是当应用程序再次被唤醒时。我的代表返回零。为什么?

SplashScreenVM.swift:

  import Foundation
protocol SplashScreenVMProtocol: class {
func fetchDataSuccessfuly()
func failedFetchData()
}
class SplashScreenVM {

weak var delegate: SplashScreenVMProtocol!
private let dataManager = DataManagement()
private let coreDataManager = CoreDataManagement()

lazy var itemsCount = coreDataManager.getRecords("Categories").count
lazy var timestamp = coreDataManager.getRecords("Timestamp")

init(){}

func setUpData() {
dataManager.fetchData(timestamp: 0 ) { (err, res) in
//dataManager.fetchData(timestamp: timestamp.count == 0 ? 0 : timestamp[0].value(forKey: "time") as! Int64) { (err, res) in
//print("categories count: \(self.coreDataManager.getRecords("Categories").count) and artciles count \(self.coreDataManager.getRecords("Articles").count)")

if(err != nil) {
print("Error: \(String(describing: err))");
self.delegate.failedFetchData()
return
}
self.coreDataManager.setTimestamp()
self.delegate.fetchDataSuccessfuly()
}
}
}

打开应用程序时我的第一个 View SplashScreenController:

    import UIKit
import Alamofire

class SplashScreenController: UIViewController, SplashScreenVMProtocol {

@IBOutlet weak var loadSpinner: UIActivityIndicatorView!

let splashScreenVM = SplashScreenVM()

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
lazy var MainController = storyBoard.instantiateViewController(withIdentifier: "MainVC")

override func viewDidLoad() {
super.viewDidLoad()


splashScreenVM.delegate = self //Looks like something wrong with this
print("apapapaap \(splashScreenVM.delegate)")
loadSpinner.hidesWhenStopped = true
loadSpinner.startAnimating()

switch Reachability.isConnectedToNetwork() {
case true:
splashScreenVM.setUpData()
case false:
self.loadSpinner.stopAnimating()
splashScreenVM.itemsCount == 0 ? print("butinai reikia intiko") : print("keliaujam i pagr.meniu")
}
}

func fetchDataSuccessfuly() {
self.loadSpinner.stopAnimating()
self.present(MainController, animated: true, completion: nil)
}

func failedFetchData() {
self.loadSpinner.stopAnimating()
if(splashScreenVM.itemsCount != 0) {
self.present(MainController, animated: true, completion: nil)
}
return
}
}

我做错了什么?我的弱变量委托(delegate):SplashScreenVMProtocol!返回零。当应用程序再次唤醒时从头开始启动应用程序是更好的方法吗?

最佳答案

您没有在applicationWillEnterForeground中保留splashScreen变量。您将splashScreen View 设置为 subview ,但splashScreen本身只是一个局部变量。当它超出范围时,该对象将被释放。

您的数据访问是异步发生的,因此当它完成时,原始的splashScreen对象已经被释放并且不再存在。

需要有东西固定在启动屏幕上。

关于ios - 调用 applicationWillEnterForeground 后重新启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52315528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com