I want to write some code to pop from the current view controller back to the root view controller. Confusingly enough, I have found numerous answer regarding this, with all sorts of syntax. (I'm guessing old answers in Objective C or different version of Swift...) Here is the simplest syntax that is currently working for me in my AppDelegate:
let navigationViewController = self.window?.rootViewController as! UINavigationController
[navigationViewController .popToRootViewController(animated: true)]
I got this far by viewing this answer: https://stackoverflow.com/a/23027260/8887398 His syntax was slighting different, which I guess is old syntax since it was from 2012, so I had to change it slightly, but it's working with my code above.
Question: I don't really understand what's going on in the 2nd line of the code. Sure, I understand it's popping back to the root view controller, but I don't understand the syntax at all. Most confusing is the code is surrounded by a pair of [ ] brackets that I don't understand at all. Next, the popToViewController starts with a dot '.', which is different than the answer I linked. I'm used to using '.' to access properties of an object, but here's it's just used stand alone without anything on the left hand side of it.
This code seems to be functioning properly for me, but which countless versions of syntax and ways to pop back to root view controller I've found across the internet, I have no idea whether what I'm doing is proper or how it works at all. All I know is "it's working right now". Please help me understand this code.
The UINavigationController class has a method inside named as popToRootViewController which takes a bool parameter named animated. If you pass animated value as true the navigation will happen with animation.
So to call the method in objective c
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:YES];
Above in first time we got a pointer to navigationController. Using that we are calling the popToRootViewControllerAnimated method inside the UINavigationController class with parameter value YES(in Objc YES is for true and NO is for false)
The same thing above can be written in swift as
let rootNavController = self.window?.rootViewController as! UINavigationController
rootNavController.popToRootViewController(animated: true)
Similar to Scenario in OBjc the above written syntax is in swift. we get the instance of navController an we call the popToRootViewController method()