更改TABBAR选择的索引

发布于 2025-02-12 14:46:42 字数 498 浏览 2 评论 0原文

查看更新时,我需要更改tabar选定的项目。因此,我有带4个项目的Tabbar,我希望在单击最后一个索引中的按钮时,查看更新和选定的项目也是最后一个(3)。但是,当我单击按钮时,所选项目会更改,并显示FirstViewController。 需要

let tabBarVC = UIStoryboard(name: "TabBarViewController", bundle: nil).instantiateViewController(withIdentifier: "CustomTabBarViewController") 
let nav = UINavigationController(rootViewController: tabBarVC)
nav.setAsRoot()

当我将选定的项目设置为fustantabbarviewController's viewDidload还可以,但是我需要,当单击按钮时,我需要,我 当塔巴尔显示时。

I need to change tabbar selected item when view updates. So, i have tabbar with 4 items, i want that when clicking on the button that is in the last index, view updates and selected item also stays the last(3). But when i am clicking on the button, the selected item changes and it shows firstviewcontroller. I am using this code in button click

let tabBarVC = UIStoryboard(name: "TabBarViewController", bundle: nil).instantiateViewController(withIdentifier: "CustomTabBarViewController") 
let nav = UINavigationController(rootViewController: tabBarVC)
nav.setAsRoot()

When i set selected item to 3 in CustomTabBarViewController's viewDidload it is okay, but i need that when clicking on button that works not always when tabbar is showing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2025-02-19 14:46:42

如果您需要刷新所有VC,

let tabBarVC = UIStoryboard(name: "TabBarViewController", bundle: nil).instantiateViewController(withIdentifier: "CustomTabBarViewController") as! CustomTabBarViewController
tabBarVC.selectedIndex = 3
let nav = UINavigationController(rootViewController: tabBarVC)
nav.setAsRoot()

但是如果您只需要刷新最后一个VC,则

let tabBarVC = self.tabBarController as! CustomTabBarViewController
let vc = // load only 3rd vc from storyboard with it's identifier
tabBarVC.viewControllers![3] = vc

If you need to refresh all the vcs

let tabBarVC = UIStoryboard(name: "TabBarViewController", bundle: nil).instantiateViewController(withIdentifier: "CustomTabBarViewController") as! CustomTabBarViewController
tabBarVC.selectedIndex = 3
let nav = UINavigationController(rootViewController: tabBarVC)
nav.setAsRoot()

But if you need to refresh only the last vc then

let tabBarVC = self.tabBarController as! CustomTabBarViewController
let vc = // load only 3rd vc from storyboard with it's identifier
tabBarVC.viewControllers![3] = vc
银河中√捞星星 2025-02-19 14:46:42

基本思想是,tabbarcontroller:syseclectViewController:selector在您的uitabbarcontroller委托中,每当用户单击选项卡项目时,都会调用。

因此,通过适当地定义该方法,您有机会在当前视图控制器被单击选项卡栏中选择的用户替换为当前视图控制器之前进行自己的处理。

因此,只需从此选择器中返回否,以防您希望防止当前视图控制器更换,即当交易正在进行时。

import UIKit

class TabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self // Delegate self to handle delegate methods.
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let selectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController)!
        if selectedIndex == 3 { // <- The index for which you don't want to set view.
            // Do anything. 
            return false
        }
        return true
    }
}

The basic idea is that the tabBarController:shouldSelectViewController: selector in your UITabBarController delegate is called whenever the user clicks on tab item.

Thus, by appropriately defining that method, you get a chance to do your own processing before the current view controller is replaced by the one the user selected by clicking in the tab bar.

So, simply return NO from this selector in case you wish to prevent the current view controller to be replaced, i.e. when a transaction is ongoing.

import UIKit

class TabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self // Delegate self to handle delegate methods.
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let selectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController)!
        if selectedIndex == 3 { // <- The index for which you don't want to set view.
            // Do anything. 
            return false
        }
        return true
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文