Reputation: 129
I have a general question: I have set up a TabBar
in my app. When I click on item 1, the TabBar shows a ViewController
(VC 1) embedded in a NavigationController
. This VC is supposed to push another VC (VC 2) when the user logs out (using firebase and addStateDidChangeListener
). It works great : When I log out the VC1 push VC 2. However when I click again on the item 1 on the tab bar. Guess what? I can see VC 1 while there is no user. I think I have to dismiss VC 1 somehow but I do not know how to implement this function. Can you help me?
Upvotes: 1
Views: 2373
Reputation: 561
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("didReceive2: ", userInfo)
Messaging.messaging().appDidReceiveMessage(userInfo)
// NotificationCenter.default.post(name: NotificationName.openRaceDAyTipsTab, object: nil)
let application = UIApplication.shared
if(application.applicationState == .active) {
print("user tapped the notification bar when the app is in foreground")
}
if(application.applicationState == .inactive) {
print("user tapped the notification bar when the app is in background")
}
guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
return
}
print("got active scene")
// handle the push notification here
if let articleId = userInfo["articleId"] as? String {
// use the articleId in your app logic
// Create an instance of ArticlesDetailViewController
let articlesDetailVC = ArticlesDetailViewController.instantiateHome()
articlesDetailVC.articlesId = articleId
if let tabBarVC = rootViewController as? UITabBarController,
let navVC = tabBarVC.selectedViewController as? UINavigationController {
navVC.pushViewController(articlesDetailVC, animated: true)
}
}
completionHandler()
}
Upvotes: -1
Reputation: 15748
When logout button is tapped remove the VC1 from navigation controller and add VC2
if var viewControllers = self.navigationController?.viewControllers {
viewControllers.removeLast()
viewControllers.append(VC2())
self.navigationController?.setViewControllers(viewControllers, animated: true)
}
When logging in again in VC2
again change view controllers
self.navigationController?.setViewControllers([VC1()], animated: true)
Logout
if var viewControllers = self.tabBarController?.viewControllers {
let newVC = UINavigationController(rootViewController: VC2())
newVC.tabBarItem = UITabBarItem(title: "Log In", image: nil, tag: 0)
viewControllers.removeFirst()
viewControllers.insert(newVC, at: 0)
self.tabBarController?.viewControllers = viewControllers
}
Login
if var viewControllers = self.tabBarController?.viewControllers {
let newVC = UINavigationController(rootViewController: VC1())
newVC.tabBarItem = UITabBarItem(title: "Home", image: nil, tag: 0)
viewControllers.removeFirst()
viewControllers.insert(newVC, at: 0)
self.tabBarController?.viewControllers = viewControllers
}
Upvotes: 3
Reputation: 101
You can override window rootviewcontroller new tabbar controller. So in future you dont have any bugs with new development (new screen hierarchy).
//Login
let navigation = UINavigationController.init(rootViewController: vc1)
let tabVC = UITabBarController()
tabVC.viewControllers?.append(navigation)
self.appDelegate?.window?.rootViewController = navigation
//Logout
let navigation = UINavigationController.init(rootViewController: vc2)
let tabVC = UITabBarController()
tabVC.viewControllers?.append(navigation)
self.appDelegate?.window?.rootViewController = navigation
Upvotes: 0