6

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.

mntruell
  • 181
  • 1
  • 13
lucadb
  • 422
  • 1
  • 6
  • 15
  • 2
    Do you have a reference to lblTotCaratteri ? If not, declare it in your @interface. If so, don't redeclare it with UILabel * (on line 2 above). Then you can call lblTotCaratteri.text = @"newtexthere". – mjhoy Aug 04 '09 at 02:27
  • It worked! I added it to the interface and of course didn't redeclare it. How can i mark your comment as the answer? Thanks again! – lucadb Aug 04 '09 at 02:33
  • I noticed your variable is called fixedSpace, but its value is actually FlexibleSpace. Is that a mistsake? – mk12 Aug 04 '09 at 17:05
  • Yes, it's a mistake. A translation one. I changed something from Italian to English before posting the code. I meant flexible. Thanks. – lucadb Aug 05 '09 at 10:09

2 Answers2

6

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";
Neil
  • 1,813
  • 1
  • 12
  • 20
  • Sorry mjhoy that I basically restated your answer in the second part. Didn't see it earlier. – Neil Aug 04 '09 at 02:38
  • As i commented before, it worked adding the label to the interface and calling it wherever i needed it. Thank you for your answer too. – lucadb Aug 04 '09 at 02:39
5

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;

Youraj Pawar
  • 51
  • 1
  • 1