ios 以编程方式切换到 TabBar 选项卡视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5413538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:20:51  来源:igfitidea点击:

Switching to a TabBar tab view programmatically?

iphoneobjective-ciosuitabbarcontroller

提问by rottendevice

Let's say I have a UIButtonin one tab view in my iPhone app, and I want to have it open a different tab in the tab bar of the TabBarController. How would I write the code to do this?

假设UIButton我的 iPhone 应用程序中有一个选项卡视图,我想让它在TabBarController. 我将如何编写代码来做到这一点?

I'm assuming I unload the existing view and load a specific tab view, but I'm not sure how to write the code exactly.

我假设我卸载了现有视图并加载了一个特定的选项卡视图,但我不确定如何准确地编写代码。

回答by Jordan

Try this code in Swift or Objective-C

在 Swift 或 Objective-C 中试试这个代码

Swift

迅速

self.tabBarController.selectedIndex = 1

Objective-C

目标-C

[self.tabBarController setSelectedIndex:1];

回答by John Smith

Note that the tabs are indexed starting from 0. So the following code snippet works

请注意,选项卡从 0 开始索引。因此以下代码片段有效

tabBarController = [[UITabBarController alloc] init];
.
.
.
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:4];

goes to the fifth tab in the bar.

转到栏中的第五个选项卡。

回答by ThomasT

My opinion is that selectedIndexor using objectAtIndexis not necessarily the best way to switch the tab. If you reorder your tabs, a hard coded index selection might mess with your former app behavior.

我的观点是selectedIndexor usingobjectAtIndex不一定是切换选项卡的最佳方式。如果您重新排序选项卡,硬编码的索引选择可能会干扰您以前的应用程序行为。

If you have the object reference of the view controller you want to switch to, you can do:

如果您有要切换到的视图控制器的对象引用,则可以执行以下操作:

tabBarController.selectedViewController = myViewController

Of course you must make sure, that myViewControllerreally is in the list of tabBarController.viewControllers.

当然,您必须确保,这myViewController确实在tabBarController.viewControllers.

回答by Alex Deem

You can simply just set the selectedIndexproperty on the UITabBarController to the appropriate index and the view will be changed just like the user tapped the tab button.

您可以简单地selectedIndex将 UITabBarController 上的属性设置为适当的索引,并且视图将像用户点击选项卡按钮一样更改。

回答by Joped

I tried what Disco S2 suggested, it was close but this is what ended up working for me. This was called after completing an action inside another tab.

我尝试了 Disco S2 建议的内容,结果很接近,但这就是最终对我有用的方法。这是在另一个选项卡内完成操作后调用的。

for (UINavigationController *controller in self.tabBarController.viewControllers)
{
    if ([controller isKindOfClass:[MyViewController class]])
    {
        [self.tabBarController setSelectedViewController:controller];
        break;
    }
}

回答by StuStirling

For cases where you may be moving the tabs, here is some code.

对于您可能正在移动选项卡的情况,这里有一些代码。

for ( UINavigationController *controller in self.tabBarController.viewControllers ) {
            if ( [[controller.childViewControllers objectAtIndex:0] isKindOfClass:[MyViewController class]]) {
                [self.tabBarController setSelectedViewController:controller];
                break;
            }
        }

回答by Json

Like Stuart Clark's solution but for Swift 3:

像斯图尔特克拉克的解决方案,但对于 Swift 3:

func setTab<T>(_ myClass: T.Type) {
    var i: Int = 0
    if let controllers = self.tabBarController?.viewControllers {
        for controller in controllers {
            if let nav = controller as? UINavigationController, nav.topViewController is T {
                break
            }
            i = i+1
        }
    }
    self.tabBarController?.selectedIndex = i
}

Use it like this:

像这样使用它:

setTab(MyViewController.self)

Please note that my tabController links to viewControllers behind navigationControllers. Without navigationControllers it would look like this:

请注意,我的 tabController 链接到导航控制器后面的视图控制器。如果没有 navigationControllers,它看起来像这样:

if let controller is T {

回答by Jin

My issue is a little different, I need to switch from one childViewController in 1st tabBar to home viewController of 2nd tabBar. I simply use the solution provided in the upstairs:

我的问题有点不同,我需要从第一个 tabBar 中的一个 childViewController 切换到第二个 tabBar 的 home viewController。我只是使用楼上提供的解决方案:

tabBarController.selectedIndex = 2

However when it switched to the home page of 2nd tabBar, the content is invisible. And when I debug, viewDidAppear, viewWillAppear, viewDidLoad, none of them is called. My solutions is to add the following code in the UITabBarController:

但是当它切换到第二个tabBar的主页时,内容是不可见的。当我调试 viewDidAppear、viewWillAppear、viewDidLoad 时,它们都没有被调用。我的解决方案是在UITabBarController中添加如下代码:

override var shouldAutomaticallyForwardAppearanceMethods: Bool 
{
    return true
}

回答by Stuart Clark

I wanted to be able to specify which tab was shown by class rather than index as I thought it made for a robust solution that was less dependant on how you wire up IB. I didn't find either Disco's or Joped's solutions to work so i created this method:

我希望能够指定按类而不是索引显示哪个选项卡,因为我认为它提供了一个健壮的解决方案,较少依赖于您如何连接 IB。我没有找到 Disco 或 Joped 的解决方案,所以我创建了这个方法:

-(void)setTab:(Class)class{
    int i = 0;
    for (UINavigationController *controller in self.tabBarContontroller.viewControllers){
        if ([controller isKindOfClass:class]){
            break;
        }
        i++;
    }
    self.tabBarContontroller.selectedIndex = i;
}

you call it like this:

你这样称呼它:

[self setTab:[YourClass class]];

Hope this is helpful to someone

希望这对某人有帮助

回答by Rakesh Singh

Use in AppDelegate.mfile:

AppDelegate.m文件中使用:

(void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController
{

     NSLog(@"Selected index: %d", tabBarController.selectedIndex);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }

    NSUInteger selectedIndex = tabBarController.selectedIndex;

    switch (selectedIndex) {

        case 0:
            NSLog(@"click me %u",self.tabBarController.selectedIndex);
            break;
        case 1:
            NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
            break;

        default:
            break;

    }

}