Customizing the More menu on a Tab bar

后端 未结 6 747
野性不改
野性不改 2020-11-27 11:58

I am using a tab bar (UITabBarController) on my app and I wish to customize the appearance of the table that appears when you click the more button. I have worked out how to

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 12:14

    I followed Ian's implementation to customize the More menu, but I was having a problem retaining the customizations after a memory warning. didReceiveMemoryWarning seems to destroy the UITableView, and when it is regenerated it gets its old dataSource back. Here's my solution:

    I replace viewDidLoad on the CustomTabBarController with this:

    - (void)viewDidLoad {
        [super viewDidLoad];
        UINavigationController* moreController = self.moreNavigationController;
        if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) {
            moreController.delegate = self;
            self.moreControllerClass = [moreController.topViewController class];
            UITableView* view = (UITableView*) moreController.topViewController.view;
            self.newDataSource = [[[MoreDataSource alloc] initWithDataSource:view.dataSource] autorelease];
        }
    }
    

    As you can see, I added a few properties for storing things I needed. Those have to be added to the header and synthesized. I also made CustomTabBarController a UINavigationControllerDelegate in the header. Here's the delegate function I added:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        if ([viewController isKindOfClass:self.moreControllerClass]) {
            UIView* view = self.moreNavigationController.topViewController.view;
            if ([view isKindOfClass:[UITableView class]]) {
                UITableView* tview = (UITableView*) view;
                tview.dataSource = self.newDataSource;
                tview.rowHeight = 81.0;
            }
        }
    }
    

    This way I make sure my custom data source is always used, because I set it that way just prior to showing the UIMoreListController, every time it's shown.

提交回复
热议问题