在我的App中的语言切换上,我需要在TabBar中访问MoreViewController的视图并更改其标题。
有人可以告诉我该怎么做吗?
非常感谢您的帮助。
列数
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
我能够使用以下代码更改第6个视图控制器(更多列表中的最后一个)的tabBar项目之一的标题:
NSArray *vcs = [(UITabBarController *)self.window.rootViewController viewControllers];
[[vcs.lastObject tabBarItem] setTitle:@"New Title"];
这是你想要做的吗?
编辑后:要在第一次设置这些标题后更改这些标题,您需要重新设置tabBar控制器的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;
}