Я пытаюсь обновить контроллер вида root с ViewController()
до fourYearPlan()
. Когда пользователь первоначально открывает приложение, они приветствуются в ViewController()
и приводят к TableViewController()
. Оттуда, когда они нажимают на tableViewCell, он приводит их к fourYearPlan()
и автоматически заменяет контроллер вида root на него. Если пользователь нажимает кнопку в fourYearPlan()
, контроллер вида root сбрасывается до ViewController()
. Как я смогу подойти к этому?
AppDelegate:
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
становится новым root контроллером просмотра из 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()
, которая сбрасывает root контроллер просмотра:
@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)
}