SignUp Button Not Working While Handling RootViewController?
I'm building an iOS app using a Storyboard. The root view controller is a Home View Controller. I'm creating the login/logout process, and it's mostly working fine, but I've got a few issues. I need to know the BEST way to set all this up. When I tap on login and logout the app works fine but when I tap on Sign up Nothing works I have created a navigation controller too. I'm just not sure what the best method is at this point.
I'm Giving you My Details
This Is MY Switcher Class For Managing View
import UIKit
class Switcher {
static func updateRootViewController() {
let status = UserDefaults.standard.bool(forKey: Constants.kUserDefaults.isSignIn)
var rootViewController : UIViewController?
#if DEBUG
print(status)
#endif
if (status == true) {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = mainStoryBoard.instantiateViewController(withIdentifier: Constants.StoryboardID.homeViewController) as! HomeViewController
rootViewController = mainTabBarController
} else {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let signInViewController = mainStoryBoard.instantiateViewController(withIdentifier: Constants.StoryboardID.signInViewController) as! SignInViewController
rootViewController = signInViewController
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = rootViewController
}
}
My App Delegate
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
IQKeyboardManager.shared.enable = true
Switcher.updateRootViewController()
return true
}
Sign UP Button
@IBAction func signUpBtnTapped(_ sender: UIButton) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
guard let signUpVC = storyBoard.instantiateViewController(withIdentifier: "RegistrationViewController") as? RegistrationViewController else {
return
}
print("Register Tapped")
self.navigationController?.pushViewController(signUpVC, animated: true)
}
Solution 1:
The issue is the your rootViewController does not have a UINavigationViewController set up, for neither of the Viewcontrollers. what you need to do is instead of instantiating like this:
if (status == true) {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = mainStoryBoard.instantiateViewController(withIdentifier: Constants.StoryboardID.homeViewController) as! HomeViewController
rootViewController = mainTabBarController
} else {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let signInViewController = mainStoryBoard.instantiateViewController(withIdentifier: Constants.StoryboardID.signInViewController) as! SignInViewController
rootViewController = signInViewController
}
You need to instantiate a UINavigationController which it's root is these like the following
if (status == true) {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = mainStoryBoard.instantiateViewController(withIdentifier: Constants.StoryboardID.homeViewController) as! HomeViewController
let navigationController = UINavigationController(rootViewController: mainTabBarController)
rootViewController = navigationController
} else {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let signInViewController = mainStoryBoard.instantiateViewController(withIdentifier: Constants.StoryboardID.signInViewController) as! SignInViewController
let navigationController = UINavigationController(rootViewController: signInViewController)
rootViewController = navigationController
}
Now your self.navigationController?.pushViewController(signUpVC, animated: true)
will work since the "navigationController" exists.