views:

301

answers:

4

Hi,

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];
viewController1 = [[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

A: 

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.

Morion
no no what I want is when I click on the first TabBarItem I will go back in my "MenuViewController" (its like having a button back in the NavigationController but I want it in the TabBarController, I see that in many app) I don't know how to put action on TabBarItem.
ludo
A: 

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.

Kai
No~~ you didn't undertand, I didn't want to use it like a NavigationBar.I already saw many application doing what I'm trying to do.My TabBarController will display 3 differents ViewController but static, no navigation, no other things, so I just want to implement a TabBarItem who will allow me to go back to my menu thats all.
ludo
I just find another application who have a TabBarItem and when you click in it, you can call. How to implement action when user click on a TabBarItem?
ludo
+1  A: 

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;
}
cidered
I will test it tomorrow and get back to you, but this sounds the perfect solution
ludo
Thats perfect, thank you~
ludo
A: 

Is this what you're trying to do?

alt text

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

[self.navigationController pushViewController:yourChildViewController animated:YES];
christo16
sorry thats not it. I don't need any UINavigationController ^^
ludo