如何在swift中检测应用程序重启后是否按下了按钮



我正在尝试确定用户是否选择了;重置";按钮。使用此代码,当应用程序打开或在后台时,它将返回到根控制器。一旦我终止应用程序并重新打开,应用程序仍然像从未按下过按钮一样。

@IBAction func resetClicked(_ sender: Any) {
UserDefaults.standard.set("delete was pressed", forKey: "deleteClicked")
print(UserDefaults.standard.string(forKey: "deleteClicked"))
let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: infoNavController.ID) as! infoNavController
(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(vc)
}

我尝试在应用程序代理中调用此UserDefault:

func applicationDidBecomeActive(_ application: UIApplication) {
let defaults = UserDefaults.standard
if let check = defaults.string(forKey: "deleteClicked") {
let  vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: infoNavController.ID) as! inftoNavController
(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(vc)
print("Delete was pressed, root controller is now infonavController (check)")
}

print("applicationDidBecomeActive")
}

但在这里没有用。我也在didfinishlaunchingwithoptions中编写了这段代码,但它也没有在那里阅读。有人对此有什么建议吗?

我确实想明白了。需要在适当的位置defaults.synchronize(),例如应用程序代理中的"场景代理"one_answers"didFinishLaunchingWithOptions">

相关内容

  • 没有找到相关文章

最新更新