0

在我的应用程序中进行语言切换时,我需要在 TabBar 中访问 MoreViewController 的视图并更改它们的标题。

谁能告诉我该怎么做?

非常感谢您的帮助。

科尔斯

4

2 回答 2

0

以下是一些可能对您有用的片段。请注意,以下所有内容都可能会在每个新的 iOS 版本中中断,这并不意味着要完成。


在用户编辑时自定义更多视图标题及其本身以及标题。

- (void)customizeTitleViewWithNavigationItem:(UINavigationItem *)navigationItem
{
    VASSERT(navigationItem != nil, @"invalid navigationItem supplied", navigationItem);
    UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectZero];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont boldSystemFontOfSize:20.0f];
    titleView.text = navigationItem.title;
    [titleView sizeToFit];
    navigationItem.titleView = titleView;
    [titleView release];
}

这需要在UINavigationController的委托中实现;

- (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated
{
    if (navigationController == tabBarController_.moreNavigationController)
    {
        if ([viewController isKindOfClass:NSClassFromString(@"UIMoreListController")])
        {
            [self customizeTitleViewWithNavigationItem:viewController.navigationItem];
        }
        else
        {
            NSLog(@"viewController (%@) does not seem to be a UIMoreListController", viewController);
        }
    }
    else
    {
        NSLog(@"navigationController (%@) does not seem to be the moreNavigationController", navigationController);
    }
}

这需要在UITabBarController的委托中实现;

- (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers 
{
    //get the second view of the upcoming tabbar-controller
    UIView *editView = [controller.view.subviews objectAtIndex:1];
    //did we get what we expected, which is a UITabBarCustomizeView?
    if (editView != nil && [editView isKindOfClass:NSClassFromString(@"UITabBarCustomizeView")])
    {   //yes->get the navigation-view
        UIView *navigationView = [editView.subviews objectAtIndex:0];
        //is that a navigationBar?
        if (navigationView != nil && [navigationView isKindOfClass:[UINavigationBar class]])
        {   //yes->...
            UINavigationBar *navigationBar = (UINavigationBar *)navigationView;
            [self customizeTitleViewWithNavigationItem:navigationBar.topItem];
        }
        else
        {
            NSLog(@"the navigationView (%@) does not seem to be a navigationBar", navigationView);
        }
    }
    else
    {
        NSLog(@"the editView (%@) does not seem to be a UITabBarCustomizeView", editView);
    }
}
于 2012-08-11T21:16:40.273 回答
0

我能够使用以下代码更改我的第 6 个视图控制器(更多列表中的最后一个)的 tabBar 项目之一的标题:

NSArray *vcs = [(UITabBarController *)self.window.rootViewController viewControllers];
[[vcs.lastObject tabBarItem] setTitle:@"New Title"]; 

这是你想做的吗?

编辑后:要在第一次设置这些标题后更改它们,您需要重新设置 tabBar 控制器的 viewControllers 属性。在此代码示例中,我将第 6 个视图控制器中的一个按钮连接到一个操作方法,该方法更改了我的 3 个控制器的标题,其中 2 个在更多列表中,一个在常规列表中。

  -(IBAction)changeNames:(id)sender {
    UITabBarController *tbc = (UITabBarController *)[[UIApplication sharedApplication] delegate].window.rootViewController;
    NSArray *vcs = tbc.viewControllers;
    [[vcs.lastObject tabBarItem] setTitle:@"New Title"];
    [[[vcs objectAtIndex:4] tabBarItem] setTitle:@"New VC"];
    [[[vcs objectAtIndex:3] tabBarItem] setTitle:@"New VC2"];
    tbc.viewControllers = tbc.viewControllers;
}
于 2012-08-11T22:12:24.703 回答