How to Go Back to the Initial View Controller in Swift

How to go back to the initial ViewController the right way?

It doesn't move to another view controller, because you are setting a simple view controller in your rootViewController.

What you need is:

let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "Initial")
let navController = UINavigationController.init(rootViewController: initialViewController)

UIApplication.shared.keyWindow?.rootViewController?.present(navController, animated: true, completion: nil)

This will create a navigationviewcontroller, so when on "Initial" you should be able to perform push, present actions of another view controllers.

back to initial view controller

At the very beginning you remove the initial view controller by doing this:

navigationController!.setViewControllers(controllers, animated: true)

All the previous viewControllers are gone now and replaced with the tabbar controller and its children.

If you want to go back to the initial controller, you will have to push the tabbar onto the stack instead of replacing everything with it. E.g.

navigationController?.pushViewController(tabbarVc, animated: true)

How can I set back to first view controller by navigation controller in code in present without segue?

I solved this by using pushViewController

            self.navigationController?.pushViewController(MyViewController, animated: true)

Swift: How to return to the initial view controller if there is no navigation controller

Set the storyboard id of your view controller. Then:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "someID")

Swift 2

self.showViewController(vc, sender: self)

Swift 3

self.show(vc, sender: self)

How to go back to the first ViewController?

1. First dismiss your current viewController by self.CurrentVC.dismiss(animated: true, completion: nil)

2. then, present/show your first or intial viewController present(firstVC,animated: true,completion: nil)

Here CurrentVC is the name for your current viewController and firstVC will be the name of your first viewController that you want to show.

How can I change initial view controller after first launch?

after your app lauch, it will call the didFinishLaunchingWithOptions method in the app delegate:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// here you have the chance to change your rootview controller
if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
UserDefaults.standard.set(true, forKey: "notFirstInApp")
window?.rootViewController = your tutorial view controller
}else{
window?.rootViewController = your main viewcontroller
}

return true
}

Return to initial view controller when user logs out

Try this,

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" 
bundle:nil];
LoginViewController *add =
[storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];

[self presentViewController:add
animated:YES
completion:nil];


Related Topics



Leave a reply



Submit