16

Well in my app I have a navigation controller with a table view, and when pressing a button a tab controller opens. In its first tab, I want the title of the navigation bar to be set.

Here is an image of my storyboard just to make sure we undestand each other.

enter image description here

This is my code that supposed to be working. I had used this code, when trying to open a single view and not a tab controller and it was working properly. So I guess I must change something.

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

Where Skicenter is the name of the class for my first tab and SkiCenterIds is the identifier of my tab controller in storyboard.

Code in Skicenter.m is:

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

    self.navigationItem.title = ski_center;        
}

but I do not see any title. So what I have done wrong? I have also tried this:

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

or

self.title=ski_center;

the ski_center has value because it is printed out normally in the NSLog.

4

9 に答える 9

39

Simple!

[self.tabBarController setTitle:@"Title"];
于 2013-11-17T10:33:14.097 に答える
17

Adding on to ghostrider's solution, in the mytabbarcontroller.m self.navigationItem.title didn't work for me. I have a NavigationController whose detail view is a TabController and this is what I have in my first tab implementation

self.navigationController.visibleViewController.title = ski_center;
于 2013-06-02T10:20:47.400 に答える
8

In Swift 4 and above

self.tabBarController?.title = "Title"
于 2018-06-25T09:21:19.970 に答える
6

Ok just so you know, it's very bad practice to put a UITabBarController inside a UINavigationController - Apple recommends for good UI design that you only should ever put a UINavigationController inside a UITabBarController and not the other way round. Either way, you should be able to change the title from the UITabBarController like this;

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

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

That should work.

于 2012-09-08T10:42:35.210 に答える
6

In swift 3

self.tabBarController?.navigationItem.title = "Your title goes here"
于 2017-12-27T06:32:08.017 に答える
5

I came across this and found a simpler solution. A new solution would be to change the navigation bar title from the landing view controllers like below

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.topItem?.title = "title";
 }
于 2019-07-10T16:41:28.753 に答える
2
 UITabBarController *tabController = (UITabBarController *)self.parentViewController;

 tabController.navigationItem.title = @"ABC";

This is working for me

From some R&D on internet

You have to pass the navigationItem up the chain. The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController. The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem

So to recap:

UINavigationController title = topViewController.navigationItem.title

UITabBarController tabTitle = selectedViewController.navigationItem.title

UIViewController title = navigationItem.title

于 2016-10-12T07:00:10.547 に答える
1

基本的に、あなたが気づかなかったかどうかはわかりませんが、私はそれを次のように機能させました:

タブバーコントローラーに新しいクラスを割り当てました。

私は次のことをしました:

アプリdelegate.hに追加します

@property (nonatomic,retain) NSString *center;

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;
于 2012-09-09T16:19:14.963 に答える
1

これを試して:

[self.navigationController setTitle:@"Live"];
于 2012-09-08T11:12:03.437 に答える