Reputation: 1304
I am working on App where Login is shown to user, after successful login user is landed on dashboard. where 4 tabs are shown to navigate to respective use.
I have to use, UITabBarController
for four tabs from dashboard and UINavigationController
for navigation from LoginView.
I set my UITabBarController to navigationController, when user successfully logged in.
Each view will have one logout button, on click of which I have to set my navigationcontroller
back to root.
Here is code sample which I have done.
My UITabBarController
class TabBarVC: UITabBarController {
let dashboardViewObj = DashboardVC()
let registerViewObj = RegisterVC()
let alertViewObj = AlertVC()
let historyViewObj = HistoryVC()
override func viewDidLoad() {
super.viewDidLoad()
self.viewControllers = [dashboardViewObj, registerViewObj,historyViewObj,alertViewObj]
// Do any additional setup after loading the view.
dashboardViewObj.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 0)
registerViewObj.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 0)
historyViewObj.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 0)
alertViewObj.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 0)
}
In my LoginViewContoller
let tabbarObj = TabBarVC()
@IBAction func loginBtnClicked(_ sender: Any) {
self.navigationController?.setViewControllers([tabbarObj], animated: true)
}
Now on click of logout button from any of these views, I need to set navigationController as root. How to do it...?
Following chart will help to understand what I need. Any help will be appeciated.
Upvotes: 1
Views: 203
Reputation: 1304
@frzi :- Thank you very much for answers and correction from comment section.
I just did what you suggested. The following line is corrected in my LoginViewController.
self.navigationController?.setViewControllers([tabbarObj], animated: true)
To updated line as
self.navigationController?.pushViewController(tabbarObj, animated: true)
And on Logout button action just added the line,
self.tabBarController?.navigationController?.popViewController(animated: true)
and it worked... Cheers J...
Upvotes: 2
Reputation: 246
When you are login all you need to put this code in your logout button action and it will redirect you to login page.
var window: UIWindow?
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
let VC = self.appDelegate.storyboard1.instantiateViewController(withIdentifier: "Your controller name") as! loginViewController
self.appDelegate.navigationController = UINavigationController(rootViewController: VC)
self.appDelegate.navigationController?.navigationBar.isHidden = true
self.appDelegate.window?.rootViewController = self.appDelegate.navigationController
self.appDelegate.window?.makeKeyAndVisible()
it will work.
Upvotes: 1
Reputation: 1909
You can use unwind segue easily instead of setting the root view controller or pop view controller or even dismiss methods!
See this for full description
Upvotes: 1