我正在尝试将根视图控制器从 更新ViewController()
为fourYearPlan()
. 当用户最初打开应用程序时,他们会受到欢迎ViewController()
并被引导到一个TableViewController()
. 从那里,当他们点击 tableViewCell 时,它将引导他们fourYearPlan()
并自动将根视图控制器更改为它。如果用户按下fourYearPlan()
根视图控制器中的按钮,则重置为ViewController()
. 我将如何解决这个问题?
应用委托:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
updateRootVC()
return true
}
// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
func updateRootVC(){
let status = UserDefaults.standard.bool(forKey: "status")
print(status)
if status == true {
let navController = UINavigationController(rootViewController: fourYearPlan())
window?.rootViewController = navController
}
else{
let navController = UINavigationController(rootViewController: ViewController())
window?.rootViewController = navController
}
window?.makeKeyAndVisible()
}
}
SceneDelegate 的一部分:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = window
appDelegate.updateRootVC()
}
ViewController()
将导致TableViewController()
,如果单击,fourYearPlan
将成为新的根视图控制器ViewController()
:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
let layout = UICollectionViewFlowLayout()
let destination = fourYearPlan(collectionViewLayout: layout)
// UserDefaults.standard.set(true, forKey: "status")
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
// appDelegate.updateRootVC()
navigationController?.pushViewController(destination, animated: true)
}
重置根视图控制器的按钮fourYearPlan()
:
@objc func addTapped(sender: UIBarButtonItem!) {
let addAlert = UIAlertController(title: "Start Over", message: "Are you sure you want to create a new plan?", preferredStyle: .alert)
addAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action:UIAlertAction) in
let defaults = UserDefaults.standard
// defaults.set(false, forKey: "status")
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
// appDelegate.updateRootVC()
let domain = Bundle.main.bundleIdentifier!
defaults.removePersistentDomain(forName: domain)
defaults.synchronize()
let destination = ViewController()
self.navigationController?.pushViewController(destination, animated: true)
}))
addAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(addAlert, animated: true, completion: nil)
}