views:

362

answers:

2

Hi,

I have a TabBar application with several nibs, most with a NavBar. It works pretty well, except for the "views" that are inside the "More" section of the tabBar.

As expected, it will put a NavBar to return to the "More" list, as well as the NavBar i've placed in the nib.

I've tried to remove the view controllers from the moreNavigationBar and put the top controller from my nib's navBar, but I get and extra view from somewhere:

- (void)viewDidLoad {    
    TestAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    UITabBarController *ctrl = appDelegate.rootController;

    UINavigationController *navCtrl = ctrl.moreNavigationController;

    [navCtrl popToRootViewControllerAnimated: NO];
    [navCtrl pushViewController: navController.topViewController animated: YES];
    navController = navCtrl;
 [super viewDidLoad];
}

My AppDelegate:

@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
 UITabBarController *rootController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;

The MainWindow nib is that of a Window-based project with a TabBarController, linked to the rootController in my app delegate.

The other nibs have a view + navigationController and I have a UITableViewController subclass as my Root View Controller.

If I could get this to work it wouldn't still solve my problem, because I want to allow the user to place this anywhere in the tabBar, so, I must have some way of knowing if there's a navigationBar.

So, my question is, how do you know if there's a navigationBar (in this case, if the tabBar's navigationBar is being shown) and, if so, how do I get my navigationController to "become" the tabBar's navigationController?

Or, if you have another idea on how to solve this problem, i'd also be appreciated :)

+1  A: 

The recommendation from apple is that you have the TabBar controller contain the Navigation controllers and not the other way around. I have a setup more or less like this, and I have the More tab hold a Nav controller, basically like this:

@interface SomethingNavViewController : UIViewController {
    UIView* aview;
    UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIView *aview;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

In the NIB, I have a separate Nav controller in the view of the more panel, I haven't replaced the tab bar item's view with a nav controller view, I've just added a Nav controller to the view.

In my implementation file, I have:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view] addSubview:[navigationController view]];
    SomeOtherController *aController = [[[SomeOtherController alloc ] initWithNibName:@"SomeOtherController" bundle:nil ] autorelease];
    aController.title = @"Artwalks";
        // lots of application logic here.
    [self.navigationController pushViewController:aController animated:YES];
    [self.navigationController setDelegate:self];
}

One key thing about this is that I have implemented the navigationController's delegate method, which is really handy when you're just inserting the nav controller. I found when I didn't do this, my views don't get viewDidAppear messages, so I implemented the protocol and added this method:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController respondsToSelector:@selector(viewDidAppear:)]) {
        [viewController viewDidAppear:animated];
    }
}

and that solved a variety of my lingering problems.

Anyway, I hope this answer gave you the detail you needed. IF it didn't, please give more details about your question. I'm not quite sure what but I get and extra view from somewhere met, but it sounds like something I encountered before I found this solution.

corprew
Thanks. Your answer actually enlightens me a little bit more, but, when I tried to implement it, I didn't get the expected result. I still get two NavigationBars when I'm within the "More" item/section.I din't understant if I need to have a nib for the "SomethingNavViewController" but I tried both ways (with and without nib). With a nib, I created one based on the View XIB, then dragged a navigationController and linked the outlet from the SomethingNavViewController to it. This seems wrong somehow.Can you create a sample project and host it somewhere so that I can take a look? Thanks.
Tiago
I've read your answer again and I found out than I've missed the NIB part. What do you mean by "added a Nav controller to the view"? I do have a TabBarController created directly in the MainWindow.xib, but I don't see where to put a NavController. I can't also see the more panel, because IB shows all the items directly, and only puts the "More" button once I run in the simulator.
Tiago
Sorry, I meant XIB every time I said NIB above. Old habits.If you can post a sample app, I'll check it out. I suspect from this comment that I don't understand fully the problem you're having.
corprew
A: 

My problem is this:

Case 1: When I have my view as a button in the TabBar, it shows up with only one navigationBar: alt text

Case 2: hen I switch the item (My nav) to the "More" section, I get 2 NavigationBars: alt text

All I have is a class with a UINavigationController pointer and outlet, linking to the NavigationController i've put in the XIB file: alt text

Now, since I'm adding the subview in the MyNav controller class, like:

- (void)viewDidLoad {
    [[self view] addSubview: navigationController.view];

    [super viewDidLoad];
}

I get that I'm adding the subview (which has a navigationController + Bar) to another view that already has a navigationBar (and controller). My question is, how do I maintain the navBar in the first case, without having 2 navBars in the second case?

You're probably bored by now, but, anyway, hope you can help me again :) And I hope it's better explained this time.

Tiago