Iphone 如何访问MoreViewController的视图并更改其标题?

Iphone 如何访问MoreViewController的视图并更改其标题?,iphone,ios,ipad,Iphone,Ios,Ipad,在我的应用程序中切换语言时,我需要访问选项卡栏中MoreViewController的视图并更改其标题 谁能告诉我怎么做 非常感谢你的帮助 Cols以下是一些可能适合您的代码片段。请注意,以下所有内容都可能在每次发布iOS新版本时中断,但这并不意味着要完成 自定义更多视图标题及其本身以及用户编辑标题时的标题 - (void)customizeTitleViewWithNavigationItem:(UINavigationItem *)navigationItem { VASSERT(

在我的应用程序中切换语言时,我需要访问选项卡栏中MoreViewController的视图并更改其标题

谁能告诉我怎么做

非常感谢你的帮助


Cols

以下是一些可能适合您的代码片段。请注意,以下所有内容都可能在每次发布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);
    }
}
- (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);
    }
}
这需要在
UITabBarController
的委托中实现

- (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);
    }
}
- (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);
    }
}

我可以使用以下代码更改我的第六个视图控制器(更多列表中的最后一个)的选项卡栏项目之一的标题:

NSArray *vcs = [(UITabBarController *)self.window.rootViewController viewControllers];
[[vcs.lastObject tabBarItem] setTitle:@"New Title"]; 
这就是你想做的吗

编辑后:要在第一次设置这些标题后更改它们,需要重新设置选项卡栏控制器的viewControllers属性。在这个代码示例中,我将第6视图控制器中的一个按钮连接到一个操作方法,该方法更改了我的3个控制器、2个控制器和1个控制器的标题

  -(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;
}

实现这一目标没有官方途径。您只能计算MoreViewController的子视图,并尝试查找您计划修改的子视图。对。我已经尝试过了,但是我仍然没有找到更改视图标题的方法。您是要更改视图本身的标题,还是要更改MoreViewController下控制器的选项卡栏按钮上的名称?这是关于更改MoreViewController下控制器的选项卡栏按钮上的名称。噢,但这些是您提供给
UITabBarController
UIViewController
的标题。为什么不简单地给他们提供正确的标题呢?嗯……我不知道该说什么,我在vcsYes中看到了我所有的视图控制器,没错。但出于某种原因,vcs包含0个元素,尽管我在“更多”下面看到了4项。你把你的代码放在哪里了?它在应用程序代理中对我有效。但后来,它不起作用了。稍后,当用户决定在应用程序中切换语言时,我需要执行此操作。基本上,我正在尝试在第六视图控制器的按钮单击处理程序中执行此操作。如果代码位于第六视图控制器的按钮单击处理程序中,请尝试是否对您有效?