How to find out a tab bar controller inside a SlideMenuController swift? - swift

I have implemented an EXSlideMenuController and added a TabBarController as a main view controller in the sliding menu controller but now I want to find out the first view controller of TabBarController which is inside the EXSlideMenuController.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let menuViewController = storyboard.instantiateViewController(withIdentifier: "menuViewController") as! MenuViewController
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarControllerID") as! TabBarController
let revealController = ExSlideMenuController(mainViewController: initialViewController, leftMenuViewController: menuViewController)
self.window?.rootViewController = revealController
self.window?.makeKeyAndVisible()
how do I find out the first view controller of tab bar controller from the ExSlideMenuController?

Here is the solution to find out the first view controller of a tab bar controller inside an ExSlideMenuController
let rootController = self.window?.rootViewController as! ExSlideMenuController
let customTabBarController = rootController.mainViewController as! CustomTabBarController
let navBarController = customTabBarController.viewControllers?.first as! UINavigationController
let storeController = navBarController.viewControllers.first as! StoreListViewController
may be it will help some other person.

Related

How to change the entry point of a Ios App?

I want to change my entry Point of my project but how I can do that? I don't use the storyboard, I do it all programmatically and I want my secondViewController to be the firstViewController.
Thanks in advance!
without storyboard start with your specific ViewController put this code in didfinishlaunchingwithOptions method in AppDelegate.
let homeVC = SecondViewController()
self.window?.rootViewController = homeVC
self.window?.makeKeyAndVisible()
with storyboard
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let homeVC = storyboard.instantiateInitialViewController()
self.window?.rootViewController = homeVC
self.window?.makeKeyAndVisible()

Issues with the TabBar and NavigationBar

I have two view controller in the TabBar. I set up like if user is logged in then it's directly show the TabBar else it's showing loginViewController. Look the Code in AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": true])
let status = UserDefaults.standard.bool(forKey: "status")
//StoryBoard Decide
if (status == false){
let storyBoard : UIStoryboard = UIStoryboard(name: "Tools", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
}else {
let storyBoard : UIStoryboard = UIStoryboard(name: "Tools", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
}}
But when it's work proper when i go through login ViewController but when user is alredy logged in it's showing the navigation bar in the HomeViewController.
This is my storyBoard Setup.
And also how to manage the Navigation with TabBar.
Because you are making new navigation controller then add tabbar as its root view.
Instead of making UINavigationController you can do this:
Replace
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
with:
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
self.window?.rootViewController = nextViewController
self.window?.makeKeyAndVisible()

Navigation Bar is shown only at the first tabBarController

I've got a tabBar with 3 screens (Home, Favorite and Settings). Use Storyboard. All these screens have a NavigationController. Navigation Bar is shown only at the first screen - Home, other have no Navigation Bar (no title, no button). Why is this happening?
Upd: I have this problem only on Simulator but on real iPhone everything is good.
My AppDelegate:
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.makeKeyAndVisible()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")
self.window?.rootViewController = initialViewController
Also I have Login page before TabBarController, going to TabBarController from Login by this method:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let singInViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")
UIApplication.shared.keyWindow?.rootViewController = singInViewController
self?.present(singInViewController, animated: true, completion: nil)
Add Each View Controller Separate Navigation Bar and attach UINavigation Controller to Tab Bar Controller.

How can i set a new Root viewController in Swift 4?

I am a rookie in Swift. I have a initial viewController within a navigationController. Inside that is a tableView. When i click on the add button item in the NavigationBar a new viewController is present modaly. Now i click a another button in the presented Vc and the presented Vc is dismiss. The root Vc is present now. But i will show a new root vc. I will set a new root Vc when dismiss the second Vc.
Can i set a new root Vc programmaticly when i dismiss a second vc?
Try this out,
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
let navigationController = UINavigationController(rootViewController: newViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController

Present ViewController from UNUserNotificationCenterDelegate

In my UNUserNotificationCenterDelegate method I want to jump the user to a specific view controller. I can easily get the VC itself from the storyboard:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "beach")
What I'm not entirely sure of though is now how to actually present it, since I don't have an object handy to call presentViewController on.
If your root is navigationVC
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(vc, animated: true)
}