swift 在UITabBarController中推送ViewController

yws3nbqq  于 2023-05-16  发布在  Swift
关注(0)|答案(3)|浏览(145)

我有一个一般性的问题:我在我的应用程序中设置了一个TabBar。当我单击项目1时,TabBar显示嵌入在NavigationController中的ViewController(VC 1)。当用户注销时(使用firebase和addStateDidChangeListener),该VC应该推送另一个VC(VC 2)。它工作得很好:当我注销VC1时,推送VC2。但是,当我再次点击选项卡栏上的项目1时。你猜怎么着?我可以看到VC1,而没有用户.我想我必须以某种方式解雇VC 1,但我不知道如何实现这个功能。你能帮我吗?

vvppvyoh

vvppvyoh1#

在导航控制器中更改视图控制器

点击注销按钮时,从导航控制器中取出VC1并添加VC2

if var viewControllers = self.navigationController?.viewControllers {
    viewControllers.removeLast()
    viewControllers.append(VC2())
    self.navigationController?.setViewControllers(viewControllers, animated: true)
}

再次登录VC2时,再次更改视图控制器

self.navigationController?.setViewControllers([VC1()], animated: true)

在标签栏控制器中更改视图控制器

退出

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
}

登录

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
}
waxmsbnn

waxmsbnn2#

你可以覆盖窗口rootviewcontroller新的选项卡控制器。因此,在未来你不会有任何错误与新的发展(新的屏幕层次结构)。

//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
8i9zcol2

8i9zcol23#

func userNotificationCenter(_ center:UNUserNotificationCenter,did接收响应: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()
}

相关问题