lucadb
lucadb

Reputation: 432

Change the text of a UILabel (UIBarButtonItem) on a toolbar programmatically

I'm a beginner iPhone developer. My code is the following:

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
UILabel *lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 15)];
lblTotCaratteri.textAlignment = UITextAlignmentCenter;
lblTotCaratteri.font = [UIFont italicSystemFontOfSize:13.0];
lblTotCaratteri.textColor = [UIColor greenColor];
lblTotCaratteri.backgroundColor = [UIColor clearColor];
lblTotCaratteri.adjustsFontSizeToFitWidth = YES;
lblTotCaratteri.text = @"0";

UIBarButtonItem *lblCaratteri = [[UIBarButtonItem alloc] initWithCustomView: lblTotCaratteri];

inserimentoToolbar.items = [NSArray arrayWithObjects:fixedSpace, lblCaratteri, fixedSpace, nil];

So in my view I have a UITextView and this toolbar created programmatically. I want to change the label text each time I add a character to the UITextView. Each time the UITextView text changes, I can display an alert each key pressed. I can't figure out how to change the label text. I hope I've explained my scenario. Sorry for my English. Greetings, Luca.

Upvotes: 6

Views: 21218

Answers (2)

Youraj Pawar
Youraj Pawar

Reputation: 51

Yo , This worked for me, I change the UIBarButtonItem title when I click on same UIBarButtonItem. Mean my need was kind of flipflop. If click first time it says "MyText1" then click again, title changes to "MyText2". Text toggles when user clicks on button.

-(IBAction)myBarButtonItem:(id)item{

 if(flag){
   flag=FALSE;
   [(UIBarButtonItem *)item setTitle:@"MyText1"];
}
else{
  flag=TRUE;
  [(UIBarButtonItem *)item setTitle:@"MyText2"];
}

NOTE: I have not created UIBarButtonItem programmatically. I created in Interface Builder and set reference of method "myBarButtonItem" to this button. So when UIBarButtonItem or say this type of button is pressed it calls method "myBarButtonItem" and sends reference of button as parameter which I further type cast to (UIBarButtonItem *) and use setTitle property to change it's text or say title.

***Do declare signature of method i.e. In your header file: -(IBAction)myBarButtonItem:(id)item;

Upvotes: 5

Neil
Neil

Reputation: 1853

One way would be, assuming inserimentoToolbar is declared in your interface:

[(UILabel *)[[toolbar.items objectAtIndex:1] view] setText:@"sometext"];

This really only works assuming your toolbar doesn't change (in terms of objects).

OR

The more ideal solution would be to put lblTotCaratteri in your interface (in your header file). Like:

@interface UntitledViewController : UIViewController {
    UILabel *lblTotCaratteri;
}

Then your included code would just like

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 15)];
lblTotCaratteri.textAlignment = UITextAlignmentCenter;
// etc etc etc

Then at any point, just use

lblTotCaratteri.text = @"sometext";

Upvotes: 6

Related Questions