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

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

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()

Tab bar and Navigation bar disappeared when navigate from Appdelegate

I use Storyboard. When I tried to navigate to another View from AppDelegate, the Tab bar and Navigation bar in that View were disappeared
Here is the code
//Some conditions here
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeView = storyBoard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController?.present(homeView, animated: true, completion: nil)
Select ViewController from the storyboard.
Go to the Editor and Embed with Navigation Controller or Tab Bar Controller
Give Storyboard ID to your Navigation Controller or Tab Bar Controller
Assign that Navigation Controller or Tab Bar Controller to Root Viewcontroller from AppDelegate.
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Root_Vc = storyBoard.instantiateViewController(withIdentifier: "RootVc")
self.window?.rootViewController?.present(Root_Vc, animated: true, completion: nil)
You are just loading the viewcontroller on to the rootview. That's why you can't see tabbar or Navigation bar. You need to present Tabbar or navigation controller in order to see it.
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeView = storyBoard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController = UINavigationController(rootViewController: homeView)
use this code it should work for you

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 to find out a tab bar controller inside a SlideMenuController 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.

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)
}