2

I have a main View that some events happen and you can click in some buttons in this view and go to another one. In that view I have a navigation item that displays a Back button. For example: Main View title = "News". When I open a new view that back button is "News". Now, I would to change that back button title to "News (8)".

I already tried this code to set the backButtonItem in the second view:

self.navigationItem.backBarButtonItem.title = @"News (1)";

but I realized that you have to set the first (main view) back button title, so I tried in the first View:

self.navigationItem.backBarButtonItem.title = @"News (1)";

it works BUT only when I click on the back button. It changes the title and make the action to back to the main view. Is there any way to "update" the back button title before click on it?

https://i.stack.imgur.com/xAHuI.png
https://i.stack.imgur.com/20mnt.png

Fabio
  • 1,757
  • 19
  • 33

2 Answers2

2

Try this :

   ClassX *obj=[[ClassX alloc]initWithNibName:@"ClassX" bundle:nil];

    self.navigationItem.title=@"News(8)";

    [self.navigationController obj animated:YES];

    [obj release];

Put it your class name in place of ClassX .and in the viewWillAppear of this class from where

you are pushing put the title code which you like to get visible on the navigation bar.

SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
Ankur
  • 98
  • 2
  • 13
  • I have to change the title after the view was loaded already. Like this I change the title before call it. – Fabio Jul 01 '13 at 11:47
0

AFAIK you can't change back button after loading the view.

So what you can do is create custom UIBarButtonItem, set background same as standart back button (you can find links here: https://stackoverflow.com/a/574863/1226304 ), and then change it's title whenever you want.

It's the easiest solution that I can see right now.

Community
  • 1
  • 1
derpoliuk
  • 1,756
  • 2
  • 27
  • 43
  • That's what I realized (impossible) but I will try something else that does not use background images. Thanks anyway! – Fabio Jul 01 '13 at 16:13
  • @Fabio If you'll find another solution, can you somehow notify me? It's quite interesting question for me. – derpoliuk Jul 01 '13 at 16:59
  • I found the answer but don't ask me why it's like this! Maybe you can tell me. I used: `self.navigationItem.backBarButtonItem.title = [NSString stringWithFormat:@"News (%i)", news_counter];` `self.navigationItem.title = [NSString stringWithFormat:@"News (%i)", news_counter];` If I use only one of these lines it does not work. I have to use both oO – Fabio Jul 02 '13 at 08:19
  • Stas Derpoliuk the comment is for you. I couldn't make break lines in the comment neither mention with @. – Fabio Jul 02 '13 at 08:25
  • I have to say that this code is called in the parent view (first view). I couldn't change the back button calling form the second view but for me is more interesting to call from the first view because every view from it will have the same back button. Anyway, the solution was fine for me. – Fabio Jul 02 '13 at 08:28
  • @Fabio My solution is for calling from second (current) view, after it's loaded. – derpoliuk Jul 02 '13 at 08:41