Navigation controller is nil after switching the rootviewcontroller - ios

I have to change / switch rootviewcontroller to user home page when login is success.
But when I change rootviewcontroller here I can't navigate to another page from buttons inside home view controller.
but when I restart the app, it works fine.
I will give the code below separately.
Login button action when user login is success (here I want to change the root view)
let mainStoryboard = UIStoryboard(name: StoryboardNames.mainStoryBoard, bundle: nil)
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: ViewController().classString()) as! ViewController
let navController = UINavigationController(rootViewController: initialViewController)
navController.isNavigationBarHidden = true
self.view.window?.rootViewController = navController
This is what I give in AppDelegate and scene delegate to check if user in logged in and navigate to root view
let mainStoryboard = UIStoryboard(name: StoryboardNames.loginStoryboard, bundle: nil)
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: LoginViewController().classString()) as! LoginViewController
let navigationController = UINavigationController(rootViewController: initialViewController)
navigationController.isNavigationBarHidden = true
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
I am using a custom tab bar view controller as home view after login...the issue is with buttons inside child views of tab bar views.

Related

Skip login screen from scene delegate in tabbed bar navigation App

I am trying to skip the login screen and go straight to the MainViewController when the user is logged in. However the problem is that I have a Tab Bar Controller and Navigation Controller between the login and the main vc. After extensive search I wrote the below code
func showMainViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: MainViewController = storyboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let navigationController = UINavigationController(rootViewController: mainViewController)
//It removes all view controllers from the navigation controller then sets the new root view controller and it pops.
window?.rootViewController = navigationController
// //Navigation bar is hidden
// navigationController.isNavigationBarHidden = true
}
However it fails to show the tab bar view controller. Any help is appreciated.
try this
func showMainViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: MainViewController = storyboard.instantiateViewController(withIdentifier: "TabBar")
window?.rootViewController = mainViewController
window?.makeKeyAndVisible()
}
you should instantiate TabBar no main viewController. since is instantiate inmediatrly is if first index or set the selected index

how to set view controller as a rootviewcontroller

I have two classes AppDelegate.swift and scene.swift and My first screen is login screen when I click login button then it should set my HomeVC.swift as rootViewController at the action of login button I mentioned this code and its not giving any error not even running
let sceneDelegate = SceneDelegate()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
sceneDelegate.window?.rootViewController = vc
window?.makeKeyAndVisible()
Can somebody guide me how to work with new file sceneDelegate.swift?
Thanks in advance
when I click login button then it should set my HomeVC.swift as rootViewController
let vc = // ...
self.view.window?.rootViewController = vc

iOS Swift: Pushing two view controllers when app launched from push notification

When a user launches the app from a push notification I present one view controller, then push another. My code to present the first VC is as follows
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeNav: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeNavController") as! UINavigationController
let homePageTableVC = mainStoryBoard.instantiateViewController(withIdentifier: String(describing: HomePageTableViewController.self)) as! HomePageTableViewController
homePageTableVC.tipToPresent = tipDay
homeNav.addChildViewController(homePageTableVC)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = homeNav
self.window?.makeKeyAndVisible()
The tipToPresent property is used by the homePageTableVC (in the viewDidLoad method) to then present the second VC, using this code:
if let tipDayToPresent = tipToPresent {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tipVC = storyboard.instantiateViewController(withIdentifier: String(describing: TipViewController.self)) as! TipViewController
tipVC.dayOfTip = tipDayToPresent
tipToPresent = nil
navigationController?.pushViewController(tipVC, animated: true)
}
This works well, but when I press the back button to return to the homePageTableVC, the navigation bar is blank. The title image, menu button, and right bar button that normally show up are not visible.
Any help is appreciated.
Good if solution by #augie works. I would suggest you should not change navigation stack when you handle push notification deep link. It should behave same as it does in normal app launch. By that way you don't need to handle any edge case and no need to set up different window.
Solution: Whenever someone click on push notification dismiss all presented controller and popToRootViewController and then navigate to desired screen.
Can you try changing this
homeNav.addChildViewController(homePageTableVC)
to this
homeNav.setViewControllers([homePageTableVC], animated: false)
I believe the problem is how you are adding your table on the navigation controller. Instead of making it your navigation's root viewcontroller you are adding it as a child vc.
Changing your code like this should work:
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// Remove type UIViewController
let homeNav = mainStoryBoard.instantiateViewController(withIdentifier: "HomeNavController") as! UINavigationController
let homePageTableVC = mainStoryBoard.instantiateViewController(withIdentifier: String(describing: HomePageTableViewController.self)) as! HomePageTableViewController
homePageTableVC.tipToPresent = tipDay
// Set controllers instead of adding child
homeNav.setViewControllers([homePageTableVC], animated: false)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = homeNav
self.window?.makeKeyAndVisible()

Swipe Back Gesture After Login in Swift

In my application, after the user is successfully logged in via his email and password in the first storyboard (Auth.storyboard), he is directed to one of the ViewControllers in the second storyboard (Main.storyboard). The problem is that user is able to swipe back to the login screen in Auth.storyboard.
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
I know that with the above code it is possible to disable this swipe back gesture but according to most people it is not recommended.
Therefore I wonder that is there a better solution to prevent swipe back gesture after a user logged in.
The best way is to present your login controller modally, then dismiss it to don't add this controller in your navigation stack.
If it's your initial controller, embed it in a different controller than the next one or remove it from navigation stack programmatically.
You have to remove the Login ViewController from the stack and you can also make your home view controller as a rootViewController:
var mainNavigationController:UINavigationController?
//After login success
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeInstance = mainStoryboard.instantiateViewControllerWithIdentifier("HomeVC")
mainNavigationController = UINavigationController(rootViewController: homeInstance)
mainNavigationController?.navigationBar.hidden = true
self.window?.rootViewController = mainNavigationController
//Appdelegate code didFinishLaunching
if getUserDefault("isUser") == "YES" {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeInstance = mainStoryboard.instantiateViewControllerWithIdentifier("HomeVC")
mainNavigationController = UINavigationController(rootViewController: homeInstance)
mainNavigationController?.navigationBar.hidden = true
self.window?.rootViewController = mainNavigationController
}
else {
mainNavigationController = window!.rootViewController as? UINavigationController
}
When the login button is clicked you must have pushed the view controller to another screen.
Try to set the view controller instead of push:
func Login(){
let control = storyboard!.instantiateViewController(withIdentifier: identifier)
navigationController?.setViewControllers([control], animated: true)
}
If you were pushing your new controller ,then instead of pushing try adding that to keywindow like this . You will not be redirected to previous window then.
let homeStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = homeStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let window = appDelegate.window {
window.rootViewController = vc
window.makeKeyAndVisible()
}

programmatically set initial view controller to tab controller swift 2

I have a tab controller on my iOS App and I want to check some conditions first and do as follow:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if condition {
globalClass.token = u
let mvc: MainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("mainView") as! MainViewController
self.window?.rootViewController = mvc
} else {
globalClass.token = ""
let mvc: LoginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
self.window?.rootViewController = mvc
}
self.window?.makeKeyAndVisible()
My problem is that when I programmatically set the initial view controller to first tab of the tab controller when it is loaded, the tab menus at bottom won't load. It just loads the view controller not tab menu.
MainViewController is the first tab of the tab view controller
Thanks,
Afshin
Try this code.
let tbc = mainStoryBoard.instantiateViewControllerWithIdentifier("tabbarStoryboardId") as! UItabBarController
self.window?.rootViewController = tbc
Thanks

Resources