How to make a controller independent in App Delegate? - swift

So in the AppDelegate.swift file I have a UINavigationController as the root view controller and a mainController as the topViewController for the navigationController. I have all the data instantiated here so it can be independent. Now, I wanted to make MenuController as my topViewController but I'm not sure how to call the mainController from here.
To be more specific, I am creating a crossword game and I've been focusing on the logic and implementation of the grid and such. Now I wanted to make a Menu as my first screen before displaying the crossword game.
MainMenu -> MenuController
CrosswordGame -> MainController
Here is the code I have without the MainMenu implementation in the AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Create a ButtonStore
let buttonStore = ButtonStore()
let levelStore = LevelStore()
// Access the MainController and set its buttonStore
let navController = window!.rootViewController as! UINavigationController
let mainController = navController.topViewController as! MainController
mainController.buttonStore = buttonStore
mainController.levelStore = levelStore
return true
}
And here is what I have now
// Create a ButtonStore
let buttonStore = ButtonStore()
let levelStore = LevelStore()
// Access the MainController and set its buttonStore
let navController = window!.rootViewController as! UINavigationController
let menuController = navController.topViewController as! MenuController
// Instantiate MainController and add buttonStore / levelStore to MainController
return true
I just don't want my Crossword Game to lose its data when pressing the back key back to the MainMenu. Another question is, is it viable to put it in the AppDelegate or instantiate the MainController in the MenuController.

//create your view controllers and set any properties
let game = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "mainControllerIdentifier")
let menu = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "menuControllerIdentifier")
//create the navigation controller
let navigationController = UINavigationController()
//set the content controllers on the nav controller
navController.setViewControllers([game, menu], animated: false)
//assign the navigation controller as the root controller of the window
window.rootViewController = navigationController

Related

Whose view is not in the window hierarchy only in First Launch

I have an Onboarding screen that I'm showing to the new users when they first open the app.
In my appDelegate I check whether is the first launch or not.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = storyboard.instantiateViewController(withIdentifier: "OnBoarding")
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "onBoardingComplete") {
initialViewController = storyboard.instantiateViewController(withIdentifier: "MainApp")
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
}
Also I have a collectionViewCell that I have some buttons and when I click them I get an Alert with informations.
Example of one button
#IBAction func guide3Btn(_ sender: Any) {
let infoVC = infoService.info(title: "Title", body: "Information")
self.window?.rootViewController?.present(infoVC, animated: true, completion: nil)
}
When the user first launches the app if he clicks the info button gets this:
Warning: Attempt to present <MyApp.InfoViewController: 0x7f91db45cfb0> on <MyApp.OnbBoardViewController: 0x7f91db506af0> whose view is not in the window hierarchy!
If the user reopens the app everything is ok. I know that when we have first launch we have onBoarding as root controller but I can't understand how to fix this.
Update
This is the infoService class. I use a new storyboard to create the alert.
class InfoService {
func info(title: String, body: String) -> InfoViewController {
let storyboard = UIStoryboard(name: "InfoStoryboard", bundle: .main)
let infoVC = storyboard.instantiateViewController(withIdentifier: "InfoVC") as! InfoViewController
infoVC.infoBody = body
infoVC.infoTitle = title
return infoVC
}
}
You can try add your storyboard instantiate code blocks to main thread using DispatchQueue.main.async like below:
I solved almost all of my whose view is not in the window hierarchy! problems.
DispatchQueue.main.async {
let infoVC = storyboard.instantiateViewController(withIdentifier: "InfoVC") as! InfoViewController
infoVC.infoBody = body
infoVC.infoTitle = title
}
return infoVC
Referenced from : https://stackoverflow.com/a/45126338/4442254

How to control multiple navigation controller in an iOS project

The structure of my project is as follows.
Initially when the user is registering or trying to login the initial navigation controller should work and after successfully registering / loggin in the user should be taken to first tab of tab bar controller. But the issue that i am facing is that i am getting 2 navigation bars in the tab bar view. Can someone guide me how to implement this in the correct way.
Thanks in advance
At some point, I would assume that the app is able to determine whether the user loggedin or not, based on that you have to set the desired root view controller for the app.
For such a case, the best place to do that is application(_:didFinishLaunchingWithOptions:) method in the AppDelegate file:
Tells the delegate that the launch process is almost done and the app
is almost ready to run.
For simplicity, let's say that you are saving isLoggedin boolean in the UserDefault, so it could be achieved like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// the flag for determining whether the user loggedin or not
let isLoggedin = UserDefaults.standard.bool(forKey: "K_isLoggedin")
// the desired initial view controller (based on the value of `isLoggedin`)
let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: isLoggedin ? "TabbarIdentifier" : "FirstNavigationIdentifier")
// setting the app rootViewController
window?.rootViewController = initialViewController
return true
}
Note that "TabbarIdentifier" represents the tabbar controller at the storyboard and also "FirstNavigationIdentifier" represents the first navigation view controller at the storyboard.
if you are unaware of how to set the view controller identifier, checking this answer should help.
Technically speaking, setting the desired root view controller means setting the rootViewController to the main window of the app (AppDelegate window).
Make one of the two navigation controllers from tabbar to initial viewcontroller and add the following in Appdelegate's didFinishLAunching
if (isDashboardVC == nil || isDashboardVC == false)
{
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "navigationmain") as! UINavigationController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
else
{
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarIntial : UITabBarController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Tabbarcontroller") as! UITabBarController
let navigationController2:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "yourViewControllerName") as! UINavigationController
let navigationController:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "yourViewControllerName") as! UINavigationController
let navigationController3:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "yourViewControllerName") as! UINavigationController
tabBarIntial.viewControllers = [navigationController2, navigationController, navigationController3]
tabBarIntial.selectedIndex = 1
}

How to put an onboarding screen on the screen even though the navigation controler is the rootView

I am making an app that includes an onboarding screen inn swift. In the AppDelegate.swift I set the navigationController as the root view controller. But I have an onboarding screen and need to add that to the code also. Whenever I run the app I get Thread 1: SIGNAL SIBAGRT. Here is my code:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [ .
UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let sb = UIStoryboard(name: "Onboarding", bundle: nil)
let initialViewController = sb.instantiateInitialViewController()
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
let navigationController = window!.rootViewController as! UINavigationController
let controller = navigationController.viewControllers[0] as! AllListsViewController
controller.dataModel = dataModel
Try that:
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(named:"Onboarding",bundle: nil)
let nvc = storyboard.instantiateInitialViewController() as? UINavigationController // since your storyboard start with a navigationController
let topVc = nvc.topViewController as? AllListsViewController // then we get the first controller associated to that navigationController
topVc?.dataModel = dataModel
window?.rootViewController = nvc // still set your navigationController as the root of your application
window?.makeKeyAndVisible()

Have ViewController appear only on first launch

I would like to have my viewController named "DoctorInformationController" appear only when the user launches the app for the first time. Ive seen some people's answers, however i would like to know how to do this in swift please!
Thank you
This code I use that will launch a viewcontoller based on first or second launch of the app.
This in the AppDelegate.
var defaults: NSUserDefaults
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let firstLaunch = defaults.boolForKey("FirstLaunch")
if firstLaunch {
print("Not first launch.")
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("navController") as! UINavigationController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
} else {
print("First launch, setting NSUserDefault.")
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("firstrunVCstoryboard") as UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
return true
}
You would need to adapt it to fit your storyboard and viewcontrollers names.
EDIT:
This how I set the value in the first view controller to indicate that first run has completed. So that it will not show again.
self.defaults.setBool(true, forKey: "FirstLaunch")
There is no way to tell if it's the first launch built into the UIKit framework, however you can do it another way.
In AppDelegate.swift, go to applicationDidFinishLaunchingWithOptions method (the first one, usually). Now, add this:
//Check for key "first_launch" in User Defaults
if let _ = NSUserDefaults.standardUserDefaults().objectForKey("first_launch") {
//Set your own global variable to be true. Then, when your ViewController
//loads, do a popup window with that DoctorViewController thingy if the
//variable is true
//Example:
isFirstLaunch = true
//Then, set "first_launch" to be a value so your app will never call this block again
NSUserDefaults.standardUserDefaults().setObject("", forKey: "first_launch")
}

How to set a new root view controller

I wonder if its possible to set a new root VC?
My app gets init with a uinavigation controller that has a table view to be the root VC.
Then from the table view I am running another segue to a login window (present modally) If you then login you end up in the red VC/account page. What I want to do now is to set the red VC to be the new root VC of the app, and remove all underlying VC's. So that I can show a menu button/icon instead of a "Back" button
I have found this but I dont understand how to use it:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let yourViewController: ViewController = storyboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.setViewControllers([yourViewController], animated: true)
But I cannot get it to work. So is it possible to make the red vc in the picture act as the new root VC.
Swift 4.2
May be you should try this
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "respectiveIdentifier") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController
Swift 4, 5, 5.1
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
UIApplication.shared.windows.first?.rootViewController = vc
UIApplication.shared.windows.first?.makeKeyAndVisible()
Swift 3 Update:-
let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = testController
Swift 4 Answer
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
UINavigationController has a viewControllers property, which is a NSArray, And you can replaced it with your own NSArray of view controllrs.
This can be done as show in below sample code.
let newViewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewControllerollerID") as! YourViewController
let customViewControllersArray : NSArray = [newViewController]
navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
And if you want to show this new root view controller you can just call UINavigationController's popToRootViewController() method.
navigationController?.popToRootViewControllerAnimated(true)
In order to get the code snippet from the original question to work, I had to make a change to the third line
let navigationController = self.navigationController!
I am using this code in an #IBAction in the view controller that precedes the new root view controller.
Using the original code, I was receiving an error saying that my view controller had no member called window. After looking at the documentation, I could find no property named window. I'm wondering if the original block of code above was intended to be used inside a UINavigationController file.
Here is the block in its entirety.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let todayViewController: TodaysFrequencyViewController = storyboard.instantiateViewControllerWithIdentifier("todaysFrequency") as! TodaysFrequencyViewController
let navigationController = self.navigationController!
navigationController.setViewControllers([todayViewControl ler], animated: true)
I wonder if its possible to set a new root VC?
Yes, it's possible. How you do it depends on the context...
My app gets init with a uinavigation controller that has a table view to be the root VC.
There are actually two things that are commonly called the "root view controller":
UIWindow has a rootViewController property, which is writeable.
UINavigationController has no rootViewController property, but it does have an initializer called -initWithRootViewController:. You can set the nav controller's "root" view controller by setting it's viewControllers property.
It sounds like you're trying to change the window's root view controller, but the code you show only changes the nav controller's viewControllers property. Try setting the window's rootViewController property directly. Understand, however, that if you take that approach then the navigation controller will go away too. If you want to keep the nav controller, on the other hand, go with your current approach.
But I cannot get it to work. So is it possible to make the red vc in the picture act as the new root VC.
More information here would be helpful. What do you mean by "cannot get it to work"? What happens, and what do you expect to happen?
For swift 4.0.
In your AppDelegate.swift file in didfinishedlaunchingWithOptions method, put the following code.
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let rootVC = MainViewController() // your custom viewController. You can instantiate using nib too. UIViewController(nib name, bundle)
let navController = UINavigationController(rootViewController: rootVC) // Integrate navigation controller programmatically if you want
window?.rootViewController = navController
return true
}
Hope it will work just fine.
For Swift 5 Users you can do this way and this will definitely work for you.
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
manageLoginSession()
guard let _ = (scene as? UIWindowScene) else { return }
}
func manageLoginSession() {
guard let window = window else {return}
if UserDefaults.standard.bool(forKey: "_key_AlreadyLogin") == true {
window.rootViewController = UIStoryboard(name: "Dashboard", bundle: nil).instantiateInitialViewController()
}else{
window.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}
}
You can use this code when you click the login button :-
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc = mainStoryboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as ViewController
UIApplication.sharedApplication().keyWindow.rootViewController = vc
How and from where are you presenting redVC?
You could make it root view controller of your UINavigationController, so you would still have ability to push and pop view controllers.
self.navigationController?.viewControllers = [self];
You can use this bit of code:
let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let customViewControllersArray : NSArray = [newViewController]
self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
self.navigationController?.pushViewController(newViewController, animated: true)
If you need to set rootViewController with some animations, here is the code:
guard let window = UIApplication.shared.keyWindow else {
return
}
guard let rootViewController = window.rootViewController else {
return
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabbar")
vc.view.frame = rootViewController.view.frame
vc.view.layoutIfNeeded()
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.rootViewController = vc
}, completion: { completed in
// maybe do something here
})
For Swift 5 and above this may work for you.
if let delegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
delegate.window?.rootViewController = newViewController
delegate.window?.makeKeyAndVisible()
}
Swift 4,5
Use this below code for RootViewController
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let navigationController = UINavigationController(rootViewController: nextViewController)
if let window = UIApplication.shared.delegate?.window {
window?.rootViewController = navigationController
}
once you are in the vc that you want to set as root, just add in your viewDidLoad:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self
*As per best practice you should check if it is already the root, if not execute the code above.
Swift 3
AppDelegate file:::
#IBAction func btnGoBack(_ sender: UIButton){
self.goToHomeVC()
}
func goToHomeVC(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"HomeVC") as! HomeVC
let navController = UINavigationController.init(rootViewController: viewController)
if let window = self.appDelegate.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(navController, animated: true, completion: nil)
}
}
You can try out this code
func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
guard let window = UIApplication.shared.keyWindow else { return }
if animated {
UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
let oldState: Bool = UIView.areAnimationsEnabled
UIView.setAnimationsEnabled(false)
window.rootViewController = rootViewController
UIView.setAnimationsEnabled(oldState)
}, completion: { (finished: Bool) -> () in
if (completion != nil) {
completion!()
}
})
} else {
window.rootViewController = rootViewController
}
}
Any view controller you want to set root just call the below function like
UIApplication.shared.setRootVC(vc)
extension UIApplication {
func setRootVC(_ vc : UIViewController){
self.windows.first?.rootViewController = vc
self.windows.first?.makeKeyAndVisible()
}
}
Just write this and you are good to go.
let sb = UIStoryboard(name: "Main", bundle: nil)
let VC = sb.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let navRootView = UINavigationController(rootViewController: VC)
self.present(navRootView, animated: true, completion: nil)
This is how you can set the nib as root view controller.
let vc = HomeViewController(nibName: "HomeViewController", bundle: nil)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = vc
window?.makeKeyAndVisible()
Link to a related question
This answer applies to usage of an existing ViewController from somewhere in the current stack without instantiating and reconfiguring a new controller.
The documentation says: The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. The new content view is configured to track the window size, changing as the window size changes. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.
Just as the documentation says: It removes all views in the stack if the rootViewController is exchanged. No matter what's with the controller. So remove the ViewController from the stack to assure its view won't be removed.
This resulted in my case in the following solution:
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
guard let pageVC = self.onboardingDelegate as? OnboardingPageViewController else { return } // my current stack is in a pageViewController, it also is my delegate
let vc = self // holding myself
pageVC.subViewControllers.removeLast() // removing myself from the list
pageVC.setViewControllers([pageVC.subViewControllers[0]], direction: .forward, animated: false, completion: nil) // remove the current presented VC
appDelegate.window?.rootViewController = vc
vc.onboardingDelegate = nil
appDelegate.window?.makeKeyAndVisible()
}
Swift 4
let storyBoard = UIStoryboard(name: "Your_Storyboard_Name", bundle:Bundle.main)
self.window = UIWindow(frame: UIScreen.main.bounds)
let yourVc = storyBoard.instantiateViewController(withIdentifier: "YourIdentifier") as? YourViewController
if let window = window {
window.rootViewController = yourVc
}
window?.makeKeyAndVisible()
Swift 4,5 and above
If you Use Multiple or single Story board you want to set Different Root view controller of Navigation Controler then I use This Method:
In My case StoryBaord Name is Auth.
func setRootToLogin(transition :CATransition) {
let storyboard = UIStoryboard(name: "Auth", bundle: nil)
let loginNav = storyboard.instantiateViewController(withIdentifier: "AuthNavigationController") as! UINavigationController
window.set(rootViewController: loginNav, withTransition: transition)
let vc = window.rootViewController as! UINavigationController
let loginvc = LoginViewController.instantiateAuth()
vc.setViewControllers([loginvc], animated: true)
}