2012-09-08 85 views
10

在我的应用程序中,我有一个带有表格视图的导航控制器,当按下按钮时,一个标签控制器打开。在第一个标签中,我想要设置导航栏的标题。在标签栏控制器内设置导航栏的标题

这是我的故事板的图像,只是为了确保我们相互对抗。

enter image description here

这是我的代码,应该是工作。当我试图打开一个视图而不是一个标签控制器,并且它正常工作时,我已经使用了这个代码。所以我想我必须改变一些东西。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
     SkiCenter *myDetViewCont = [[SkiCenter alloc] initWithNibName:@"SkiCenter" bundle:[NSBundle mainBundle]]; 
     myDetViewCont.ski_center = [centers objectAtIndex:indexPath.row]; 
     UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
     [[self navigationController] pushViewController:[storyBoard instantiateViewControllerWithIdentifier:@"SkiCenterIds"] animated:YES]; 
     //[self.navigationController pushViewController:myDetViewCont animated:YES]; // "Pushing the controller on the screen" 
     [myDetViewCont release]; // releasing controller from the memory 
     myDetViewCont = nil;   
    } 

哪里Skicenter是类我的第一个选项卡,SkiCenterIds的名字是我的故事板标签控制器的标识符。

守则Skicenter .M是:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    self.navigationItem.title = ski_center;   
} 

,但我没有看到任何头衔。那么我做错了什么? 我还试图这样:

[self.navigationController setTitle:@"Live"]; 

self.title=ski_center; 

的ski_center具有价值,因为它是在正常的NSLog打印出来。

+0

您使用的导航控制器内的标签栏控制器? –

+0

是的,他这样做 – Fab1n

回答

1

基本上我不知道你是否注意到我以前不,但我得到了它是这样工作的:

我指定我的标签栏控制器的新类。

我做了以下内容:

它添加到应用delegate.h

​​

它添加到mainmap.m

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    delegate.center=[centers objectAtIndex:indexPath.row]; 

mytabbarcontroller.m

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    ski_center=delegate.center; 

    self.navigationItem.title = ski_center; 
5

好只是让你知道,这是非常不好的做法,把UITabBarControllerUINavigationController - 苹果建议您只应该永远把UINavigationControllerUITabBarController,而不是倒过来良好的UI设计。无论如何,你应该可以像这样改变UITabBarController的标题;

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [[self.navigationController.viewControllers objectAtIndex:0] setTitle:ski_center];   
} 

这应该工作。

+0

不工作。导航栏没有标题。 – ghostrider

+0

我已经更新了我的答案,请看看是否有效。 – Wasim

+0

它也没有工作。你建议我做什么?我尝试过这种结构,因为从表格中我想打开一个标签菜单,而不是打开的第一个屏幕作为标签。 – ghostrider

1

试试这个:

[self.navigationController setTitle:@"Live"]; 
+0

它没有工作。导航栏没有标题。 – ghostrider

12

添加到ghostrider的解决方案,在mytabbarcontroller.m self.navigationItem.title没有为我工作。我有一个NavigationController,其详细视图是一个TabController,这是我在我的第一个标签实现中的东西

self.navigationController.visibleViewController.title = ski_center; 
+0

这是为我工作的唯一解决方案。我设置 - UITabbarController(这是根视图控制器)内的UINavigationControllers。不知何故,所有其他明显的解决方案根本无法工作。 – Ingweland

+0

这应该是正确的答案。做得很好。谢谢您的帮助。 –

32

简单!

[self.tabBarController setTitle:@"Title"]; 
+0

标题文本在视图控制器之间切换时消失 –

2
UITabBarController *tabController = (UITabBarController *)self.parentViewController; 

tabController.navigationItem.title = @"ABC"; 

这是为我工作

从一些[R & d互联网

上你必须通过navigationItem环比上涨。 UINavigationController显示属于它的topViewController的navigationItem,它是UITabBarController。 UITabBarController在其选项卡中显示navigationItem标题。所以,你需要做的是确保该tabBarController的navigationItem是它的selectedViewController的navigationItem

因此,要回顾:

UINavigationController的标题= topViewController.navigationItem.title

的UITabBarController tabTitle = selectedViewController .navigationItem.title

UIViewController title = navigationItem.title

1

在SWIFT 3

self.tabBarController?.navigationItem.title = "Your title goes here"