简体   繁体   English

当iOS7上的selectedIndex值发生变化时,TabBar会消失

[英]TabBar disappears when selectedIndex value changes on iOS 7

I have an iOS application which was build with 6.1 base sdk. 我有一个iOS应用程序,使用6.1 base sdk构建。 Now I am moving the application to next SDK 7.0. 现在我将应用程序移动到下一个SDK 7.0。 I have a tabbar which has 5 items on it. 我有一个tabbar,上面有5个项目。 Each tab is a navigation controller. 每个选项卡都是导航控制器。 Also, each view controller which is pushed into these navigation controllers hides the tab bar with the method 此外,推入这些导航控制器的每个视图控制器都会使用该方法隐藏标签栏

[self setHidesBottomBarWhenPushed:YES];

When I am at the second tab, i press a button and after a web connection, it pops to root view controller and updates the selectedIndex of tabbarcontroller. 当我在第二个选项卡时,我按下一个按钮,在网络连接后,它会弹出到根视图控制器并更新tabbarcontroller的selectedIndex。 The main tabbar controller is accessed with its reference in appdelegate. 可以通过appdelegate中的引用访问主tabbar控制器。 The thing happening is when selected index is 1 and then I set it as 2 the tab bar disappears. 发生的事情是当选择的索引是1然后我将其设置为2标签栏消失。 However, when it is 2 and i set it 2 again there is no problem. 然而,当它是2并且我再次设置它2时没有问题。

[self.application.tabBarController setSelectedIndex:2];

This problem only occurs in iOS 7 simulator. 此问题仅发生在iOS 7模拟器中。 When I simulate on iOS 6 simulator it is not happening. 当我在iOS 6模拟器上进行模拟时,它不会发生。 Also my tabbar items are custom, i don't know if it has something to do with it. 我的tabbar项目也是自定义的,我不知道它是否与它有关。

What might be the problem? 可能是什么问题?

I found the solution. 我找到了解决方案。 My code was the following at first. 我的代码起初是以下内容。 Both navigation controllers in the tabbar controller popped to root view controller. 标签栏控制器中的两个导航控制器都弹出到根视图控制器。

[self.navigationController popToRootViewControllerAnimated:YES];
[self.application.tabBarController setSelectedIndex:2];
[self.application.tabBarController.secondTabNavigationController popToRootViewControllerAnimated:YES];

When I rearranged the orders of popToRootViewController methods, the problem disappeared. 当我重新安排popToRootViewController方法的顺序时,问题就消失了。

[self.application.tabBarController setSelectedIndex:2];
[self.application.tabBarController.secondTabNavigationController popToRootViewControllerAnimated:YES];
[self.navigationController popToRootViewControllerAnimated:YES];

The first one works fine in iOS 6 devices but it does not in iOS 7 devices. 第一个在iOS 6设备中运行良好,但在iOS 7设备中没有。 Second one works for all. 第二个适用于所有人。

If you do need to to first pop-to-root-VC and only then switch to another tab bar there is a great workaround for it by using false in the animated param: 如果你确实需要首先使用pop-to-root-VC,然后切换到另一个标签栏,那么在动画参数中使用false会有一个很好的解决方法:

// Assuming the current selected index might be a non-zero value
tabBarController.popToRootViewControllerAnimated(false)
tabBarController.selectedIndex = 0

This way you can first popToRootViewControllerAnimated and only then programatically switch to your desired tab bar. 这样你就可以先popToRootViewControllerAnimated然后以编程方式切换到你想要的标签栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM