以编程方式更改工具栏上的UILabel(UIBarButtonItem)的文本 [英] Change the text of a UILabel (UIBarButtonItem) on a toolbar programmatically
问题描述
我是初学的iPhone开发者。
我的代码如下:
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];
所以在我看来我有一个UITextView,这个工具栏以编程方式创建。
我想在每次向UITextView添加字符时更改标签文本。
每次UITextView文本更改时,我都会显示每个按键的警报。
我无法弄清楚如何更改标签文字。
我希望我已经解释了我的情景。
对不起我的英文。
Greetings,Luca。
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.
推荐答案
一种方法是,假设在你的界面中声明了inserimentoToolbar:
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).
或
更理想的解决方案是将lblTotCaratteri放入您的界面(在您的界面中)头文件)。喜欢:
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";
这篇关于以编程方式更改工具栏上的UILabel(UIBarButtonItem)的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!