Search code examples
swiftxcodestoryboardsegueuistoryboardsegue

How do I close the intermediate controller in the application?


I am making a prototype of an application.

When the user opens the application, he gets to the main screen, on which the button "Login", after clicking on this button, the user opens a new window with one button "SignIn", after which the user gets to the third screen.

On the third screen, there is a "SignOut" button, which, when pressed, should return to the main screen of the application.

Example picture below:

Sample application

I suppose that when switching from the second to the third screen, the second screen should be deleted so that only the main screen and the third screen are in the application's memory, and when the SignOut button is pressed, the user removes the third screen, and he only has the main screen in memory.

How to do it right?

MARK - At the moment I am using segue to move from one screen to another, and unwindSegue to close the screen. (Also, I'm teaching that I might need a dismiss, but I'm not sure about that)


Solution

  • Use UINavigationController:

    1. Sets your first controller as the rootViewController property:

    With code in your AppDelegate.swift in didFinishLaunchingWithOptions method:

    let navigationController = UINavigationController(rootViewController: your_controller_1)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible() 
    

    or if you use storyboard, set the entry point with your firstViewController and embeds it inside a UINavigationController

    1. Then play with your navigationController self.navigationController.push(...) to navigate to your second and third screen.

    2. Use the func popToRootViewController(animated: Bool) -> [UIViewController]? of UINavigationController to go to the rootViewController which is your first controller.

    https://developer.apple.com/documentation/uikit/uinavigationcontroller