13

In UITableView I configure self.navigationItem.leftBarButtonItem with a button. Now I want to reload this button to update text on this button, but it's on Navigation bar. I can update UITableView by

 [self.tableView reloadData]

but How to update Navigation bar?

Update: I'm NOT init the title of button, but reload it. So any init code (such as self.navigationItem.leftBarButtonItem.title = @"balabala") doesn't work, because it does NOT take effect immediately.

NANNAV
  • 4,875
  • 4
  • 32
  • 50
Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63
  • Can you post some more code around what you've already tried? Is it called from the main thread? – jszumski Apr 17 '15 at 18:49
  • 1
    I have similar problem, after clicking refresh button I want to set title in navigation item including last update timestamp. I have tried calling `[titleView setNeedsDisplay];`, also on main thread, but title does not update for few seconds or until any segue is performed, while `[self.tableView reloadData]` reloads just fine. – dev-null-dweller Apr 21 '15 at 11:17
  • Hi dev-null, is self the UIViewController that's currently displayed by the navigation controller? If so, you should be able (as stated in some answers) to change the title by using `self.navigationItem.title = @"New TITLE!";` If this doesn't work, there might have some explanations (like `self` is not the one the navigationController is currently displaying in it's container – Marc-Alexandre Bérubé Apr 23 '15 at 15:12
  • `self` is my UiTableViewController, embedded in navigation controller. When I'm changing title from other controllers (in `prepareForSegue`), target view is shown with correct title, but when currently displayed view controller tries to change title, it needs few seconds to show new value in navigation bar. – dev-null-dweller Apr 24 '15 at 07:50

8 Answers8

3

Just change the text on the leftBarButtonItem?

self.navigationItem.leftBarButtonItem.title = @"Back"; // or whatever text you want
Nandeep Mali
  • 4,456
  • 1
  • 25
  • 34
2

Try this

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                               initWithTitle:@"Title" 
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(OnClick:)];
self.navigationItem.leftBarButtonItem = btnBack;

Hope this helps you..

EDIT :-

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle: @"Title" style: UIBarButtonItemStyleBordered target:self action:@selector(OnClick:)];

[[self navigationItem] setBackBarButtonItem: btnBack];
P.J
  • 6,547
  • 9
  • 44
  • 74
  • @ybbaigo I added another way, try with that, let me know if that works? – P.J Feb 02 '13 at 11:43
  • thanks very much, but under my understanding, backBar will NOT display on the navigation bar unless push a view, I want to display this button on the navigation bar all the time. – Yuwen Yan Feb 02 '13 at 11:50
  • No, when you will call above code and if you have used navigation controller, this code will change your navigation controller button text to which you set to btnBack. Note here I am not creating navigation bar, I am just creating a UIBarButtonItem. Let me know if you have any doubts.. – P.J Feb 02 '13 at 11:59
  • sorry for take a long time to reply. there are one thing I want to confirm with you: what's the difference between your two methods? from leftBarButtonItem to backBarButtonItem? With your second method(backBarButtonItem), the button is NOT display on the navigation bar – Yuwen Yan Feb 02 '13 at 12:33
  • what do you mean by promote? – P.J Feb 04 '13 at 07:00
1

Did you try setting the text, using

self.navigationItem.leftBarButtonItem.title
d4Rk
  • 6,622
  • 5
  • 46
  • 60
1

You need to use the following statement:

self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];  

In other words you need the setLeftBarButtonItem:animated message instead of the standard one. This will reload the button text with animation. Same applies for the back button if that is what you are interested in changing at some point.

iansari
  • 354
  • 3
  • 12
1
 @interface Myviewcontroller : UIViewController
{
       UIBarButtonItem *barBtnBack;
       UIButton *btnQuestion;
}

//ViewdidLoad

 btnQuestion = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
        [btnQuestion setBackgroundImage:[UIImage imageNamed:@"question-mark2x"] forState:UIControlStateNormal];
        [btnQuestion addTarget:self action:@selector(goBacktomain)
              forControlEvents:UIControlEventTouchUpInside];
        barBtnBack =[[UIBarButtonItem alloc] initWithCustomView:btnQuestion];
    [self.navigationItem setLeftBarButtonItem:barBtnBack];

note- change btnQuestion title when you want to change navigationnar button title

bLacK hoLE
  • 781
  • 1
  • 7
  • 20
1

If you want to change the NavigationBar title, try 1 of these:

[self.navigationController.navigationItem setTitle:@"Title"];
[self.navigationItem setTitle:@"Title"];
[self setTitle:@"Title"];

If you wanna change button titles, put this in the same class:

- (void)setLeftBarButtonTitle:(NSString *)title action:(SEL)selector
{
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
    [self.navigationItem setLeftBarButtonItem:leftButton];
}

- (void)setRightBarButtonTitle:(NSString *)title action:(SEL)selector
{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
    [self.navigationItem setRightBarButtonItem:rightButton];
}

Examples to set button title and actions:

// Change button titles and actions any time
[self setLeftBarButtonTitle:@"Back" action:@selector(goBack)];
[self setRightBarButtonTitle:@"Something" action:@selector(doSomething:)];

// Change button titles with no actions
[self setLeftBarButtonTitle:@"Nothing" action:nil];
[self setRightBarButtonTitle:@"Nothing" action:nil];

// Remove buttons from navigation bar
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:nil];
emotality
  • 12,795
  • 4
  • 39
  • 60
1

Try this:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"your_title"
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(backBtnPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;
ρss
  • 5,115
  • 8
  • 43
  • 73
iMRahib
  • 532
  • 4
  • 4
0

I solved it in Swift, but if the keyboard is shown, it hides.

navigationController?.loadView()