我们已经在应用程序的AppDelegate类中处理了内存警告警报。当调用“ applicationDidReceiveMemoryWarning”功能时,它将导航到主屏幕,但未显示警报? 我将在下面添加我的代码。谁能帮我吗?
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
resetWhenMemoryWarningPresented()
}
// To get the current view controller
func getCurrentViewController(_ vc: UIViewController) -> UIViewController? {
if let presentViewControllers = vc.presentedViewController {
return getCurrentViewController(presentViewControllers)
}
else if let splitViewControllers = vc as? UISplitViewController, splitViewControllers.viewControllers.count > 0 {
return getCurrentViewController(splitViewControllers.viewControllers.last!)
}
else if let navigationControllers = vc as? UINavigationController, navigationControllers.viewControllers.count > 0 {
return getCurrentViewController(navigationControllers.topViewController!)
}
else if let tabBarViewControllers = vc as? UITabBarController {
if let selectedViewController = tabBarViewControllers.selectedViewController {
return getCurrentViewController(selectedViewController)
}
}
return vc
}
public func resetWhenMemoryWarningPresented() {
guard let rootViewControllers = self.window?.rootViewController else {
return
}
if let viewControllers = getCurrentViewController(rootViewControllers) {
weak var controller = viewControllers
print("VIEWCONTROLLER NAME \(String(describing: controller))")
let window = UIApplication.shared.keyWindow!
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
weak var viewController = storyboard.instantiateViewController(withIdentifier: "Navigation") as? UINavigationController
window.rootViewController = viewController
window.makeKeyAndVisible()
viewController?.showConfirmAlert(title: "Memory Warning", message: "Your phone is running on low memory. Please close other apps before continuing or you may experience unexpected behaviour", buttonTitle: "OK", buttonStyle: .default) { (action) in
controller = nil
viewController = nil
}
}
}
答案 0 :(得分:2)
您不应要求用户关闭某些应用程序,而应清除didReceiveMemoryWarning方法中的变量。看看这个StackOverflow帖子:How to implement didReceiveMemoryWarning?