开发者

Switch/Navigate between Tabs in TabBarController

S开发者_高级运维o I have 2 tabs in my tab bar controller.

I have a button in Tab 2, and if I click that, control needs to be passed to Tab 1, and it should become the present view controller.

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

I just need it to go to tab 1 when I click the button.


When you click the button, just have it send the following

self.tabBarController.selectedIndex = 0;

That will tell it to switch to the first tab in its index (Tab 1)

If you want to add animation to the change, you'll use transitionFromView:toView:duration:options:completion:. And to implement this, you'll use something like this:

[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;
        }
    }];

The options give you control how it transitions, and there's a nice list of all the stuff you can do. Then the completion block lets you specify what to do once you're finished. In this case, it will switch to tab 1 so it is the new primary controller


You can set your UITabBarController's selectedIndex or selectedViewController property to programmatically change the current tab.


Would you be able to set the selectedIndex property on your UITabBarController?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜