How to reset state of initial ViewController?
You can implement viewWillAppear
in VC1, and then remove all elements of the image Array. This way, when you tap back on VC2, the VC1 will have it's image Array empty.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Images.removeAll()
}
Btw, consider renaming your instance variables, lowercasing the initial character, it's a good practice.
Change the ViewController and restart app
You can create a delegate protocol gets called whenever the isNewUser value gets changed and call it's delegate function inside the scene class in order to present the view controller based on the value. Like Below:
inside you SceneDelegate class :
// don't forget to assign the RestartApp Delegate here
class SceneDelegate: UIResponder, UIWindowSceneDelegate, RestartApp {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
restartApp()
}
and create the restartApp function inside sceneDelegate as well
func restartApp() {
let isNewUser = UserDefaults.standard.bool(forKey: "Key")
if isNewUser == true {
window?.rootViewController = ViewController()
} else {
window?.rootViewController = ViewController2()
}
}
at the end where you want the restart function gets called create the protocol and it's delegate call like below:
protocol RestartApp {
func restartApp()
}
class ViewController2: UIViewController {
var delegate: RestartApp?
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.set(true, forKey: "Key")
view.backgroundColor = .yellow
print("VC2")
self.delegate?.restartApp() // this should be called wherever you want to restart the app ( inside an action or a function depends on your need )
}
}
To sum up , what happens here that the app start it calls willConnectTo function inside SceneDelegate class and this one calls the restart app and call the VC based on the last value save in IsNewUser , then inside you VC every time you call the self.delegate.restartApp() , it calls the restart func inside the SceneDelegate and call the VC based on your isNewUser Value .
iOS Swift 4 Restart Navigation controller
You need
// this inside end_click
let vc = self.storyboard!.instantiateViewController(withIdentifier:"StepOneId")
self.navigationController!.setViewControllers([vc], animated: true)
How to reset root view controller
You can do this by setting the instance of TabBarController to rootViewController on logout action.
Swift 3:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyBoard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tabBarController
UIApplication.shared.keyWindow?.makeKeyAndVisible()
Objective C:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"TabBarController"];
[[[UIApplication sharedApplication] keyWindow] setRootViewController:tabBarController];
[[[UIApplication sharedApplication] keyWindow] makeKeyAndVisible];
Related Topics
Scenekit - Scntext Centering Incorrectly
How to Get User Input in Apple's Swift Language in a Command Line Tool
How to Resolve This Build Issue - Cannot Assign to Property: 'Date' Is a Get Only Property
Key-Value Coding (Kvc) with Array/Dictionary in Swift
With Firebase, Swift Removeobserver(Withhandle Does Not Remove the Observer
Watchos3 Complication That Launches App
Is It Possible in Swift to Add Variables to an Object at Runtime
Convert Timestamp String with Epochal Time and Timezone into Nsdate
Swift Programming Nserrorpointer Error etc
Swift Error: Guard Body Must Not Fall Through
Swift Property Observer in Protocol Extension
Failed to Get Descriptors for Extensionbundleid
Swift Realm, Load the Pre-Populated Database the Right Way
Include an Extension for a Class Only If iOS11 Is Available
Error: Bool Is Not Convertible to Void:
Common Equatable Class on Swift