开发者

Implement Back action into UITabBarController

I have a controllerView (MenuControllerView) with a button inside, when I click on the button a new ViewController will appear with a TabBarController created programmatically like this:


UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];

tabBarController = [[UITabBarController alloc] init];
viewCo开发者_JAVA技巧ntroller1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
viewController4 = [[ViewController4 alloc] init];

tabBarController,viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 ,viewController4, nil];
[[self tabBarController] setSelectedIndex:1];
[topView addSubView:[tabBarController view]];

Instead of displaying ViewController1 for the first button Item, I want to put an action Back in it to return to my MenuViewController, but I don't know how how to do it.

Thanks


Have you considered presenting the UITabBarController as a modal view controller and implementing UITabBarControllerDelegate? e.g. this seems to work for me (I make the third tab return to MenuViewController here):

@interface MenuViewController : UIViewController <UITabBarControllerDelegate>
...

- (IBAction) onButtonPressed:(id)sender
{

    UITabBarController* tabBarController = [[UITabBarController alloc] init];
    viewController1 = [[ViewController1 alloc] init];
    viewController2 = [[ViewController2 alloc] init];
    viewController3 = [[ViewController3 alloc] init];

    tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 , nil];
    [[self tabBarController] setSelectedIndex:1];

    tabBarController.delegate = self;
    [self presentModalViewController:tabBarController animated:NO];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
    if (viewController == viewController3)
    {
        [self dismissModalViewControllerAnimated:NO];
        return NO;
    }
    return YES;
}


I doubt that this approach is a good one. You'll gonna break typical iPhone behaviour which will confuse users. The TabBarController is designed (functionally and technically) to change between views while a NavigationController is for pushing and popping views (go forth and back). Of course you can combine those (which is not always easy), but you shouldn't use TabBar as NavigationBar.


if I understand right, you can just remove your tabbar's view from superview. smth like

[[tabBarController view] removeFromSuperview];

if you just want to handle selection of tabbar item, you can use tabBar:didSelectItem: method of th UITabBarDelegate protocol.


Is this what you're trying to do?

Implement Back action into UITabBarController

This automatically created with a UINavigationController upon pushing to a child view controller.

[self.navigationController pushViewController:yourChildViewController animated:YES];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜