问题描述
|
所以我的标签栏控制器中有2个标签。
我在选项卡2中有一个按钮,如果单击该按钮,则需要将控件传递给选项卡1,它应该成为当前的视图控制器。
//I should not use navigation for this because the view would navigate
the present view to the view in tab 1 ; but the tab will still indicate Tab 2//
单击按钮时,只需将其转到选项卡1。
解决方法
当您单击按钮时,只需发送以下内容
self.tabBarController.selectedIndex = 0;
这将告诉它切换到索引中的第一个选项卡(表1)
如果要向更改添加动画,请使用transitionFromView:toView:duration:options:completion:
。为了实现这一点,您将使用以下代码:
[UIView transitionFromView:self.view
toView:[[self.tabBarController.viewControllers objectAtIndex:0] view]
duration:1 /*or whatever time you want*/
options:/*specify your animation transition here,they are found in the UIView documentation*/
completion:(void (^)(BOOL finished))completion:^(BOOL finished) {
if (finished) {
tabBarController.selectedIndex = 0;
}
}];
这些选项使您可以控制它的过渡方式,并且列出了您可以做的所有事情。然后,完成模块可让您指定完成后的操作。在这种情况下,它将切换到选项卡1,因此它是新的主控制器
, 您可以将UITabBarController的selectedIndex
或selectedViewController
属性设置为以编程方式更改当前选项卡。
, 您是否可以在UITabBarController上设置selectedIndex属性?