UITabBarController temporarily missing item title and image in More Navigation Controller

1.3k views Asked by At

"Has this ever happened to you?"

(Note: The following is ARC-friendly code, hence no autorelease call or retain/release pairs.)

#pragma mark - UIViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    // "We interrupt this designated initializer to go do our own thing ..."
    return [self init];
}

#pragma mark - NSObject

- (id)init {
    // Why hello there, superclass designated initializer.
    if ((self = [super initWithNibName:@"YourNibHere" bundle:nil])) {

        // We're in a nav controller, so set the title.
        self.navigationItem.title = @"Item Title";

        // "Someone set up us the tab bar!"
        [self setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Item Title" image:[UIImage imageNamed:@"itemIcon.png"] tag:itemTag]];
    }
    return self;
}

So ... this is the top UIViewController in a UINavigationController, which in turn is in a UITabBarController at some index. The classic turducken of Cocoa view controllers, if you will.

You tap the tab bar item, context changes to the top UIViewController, and life is wonderful.

Ahh, but what if you want to programmatically switch to this from a view controller under a different (tab bar) index?

Seems easy enough. Just use -selectedViewController: and pass in the UINavigationController (not the UIViewController) you want to switch to. "Mostly harmless."

This works every time ... except I run into a really annoying quirk whenever that UINavigationController is tucked away beneath the infamous More navigation controller (used by iOS when there are more than five tab bar items in play).

The quirk? When I tap the back button ("More") to pop the UIViewController (it's no longer topmost due to the More sitch, y'see), the imageView and textLabel in the table view cell identifying this tab bar item are missing! As in not there. Just an empty cell.

If I scroll the table view cell offscreen and back on, or otherwise move away from and back to the More navigation controller, then it appears. :(

Wha' happen? Inquiring minds want to know ...

1

There are 1 answers

0
app_ On

I made a fix for this. It's not pretty but it's something.

Extend the UITabBarController:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.moreNavigationController.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    //Fix for not showing the selected item in the tableview.
    if ([navigationController.topViewController isKindOfClass:NSClassFromString(@"UIMoreListController")]) {
        [navigationController.topViewController.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            if ([obj isKindOfClass:[UITableView class]]) {
            *stop = YES;
                UITableView *tableView = (UITableView *)obj;
                [tableView reloadData];
            }
        }];
    }
}

This way the tableview will reloaded when it becomes visible. You'll see the cell appear in the transition but that's not a problem for me.