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
Cllocationcoordinate2D Can't Be Instantiated
In Swift 3, What Is a Way to Compare Two Closures
Swift: Print Decimal Precision of Division
How to Include .Swift File from Other .Swift File in an Immediate Mode
Swift Generics: Requiring Addition and Multiplication Abilities of a Type
Swift: Determine What Object Called a Function
How to Make the Scroll of a Tableview Inside Scrollview Behave Naturally
Multiple Type Constraints in Swift
Using as a Concrete Type Conforming to Protocol Anyobject Is Not Supported
How to Check If Time Is Within a Specific Range in Swift
How to Find the Number of Days in Given Month and Year Using Swift
Dismiss Keyboard with a Uitextview
How to Scale Text to Fit Parent View with Swiftui
What Does an Ampersand (&) Mean in the Swift Language
How to Convert Range in Nsrange
Determining If Swift Dictionary Contains Key and Obtaining Any of Its Values