在我的应用程序中的语言切换上,我需要访问TabBar中MoreViewController的视图并更改它们的标题。如何访问MoreViewController的视图并更改其标题?
任何人都可以请告诉我该怎么做?
你的帮助是非常赞赏。
列数
在我的应用程序中的语言切换上,我需要访问TabBar中MoreViewController的视图并更改它们的标题。如何访问MoreViewController的视图并更改其标题?
任何人都可以请告诉我该怎么做?
你的帮助是非常赞赏。
列数
下面是一些片段,可能为你工作。请注意,下面的所有内容都有可能在每个新的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);
}
}
我可以用下面的代码来改变我的第六视图控制器使用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;
}
嗯...不知道该说些什么,我看到我所有的视图控制器在vcs – rdelmar 2012-08-12 05:45:40
是的,那是对的。但由于某种原因,vcs包含0个元素,尽管我在“More”下面看到4个项目。你把你的代码放在哪里? – user1006117 2012-08-12 05:50:50
好的,在哪个方法是你的代码? – user1006117 2012-08-12 05:51:38
有没有官方的方式来实现这一点。您只能评估MoreViewController的子视图并尝试找到您计划修改的子视图。 – Till 2012-08-11 21:04:41
对。我已经尝试过,但我还没有找到改变视图标题的方法。 – user1006117 2012-08-11 21:08:21
您是否想更改视图本身的标题,或更改MoreViewController下控制器的标签栏按钮的名称? – rdelmar 2012-08-11 21:20:17