gpt4 book ai didi

iphone - 以编程方式更改工具栏上 UILabel (UIBarButtonItem) 的文本

转载 作者:行者123 更新时间:2023-12-03 18:43:05 26 4
gpt4 key购买 nike

我是一名 iPhone 开发新手。我的代码如下:

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 文本更改时,我都可以在按下每个键时显示警报。我不知道如何更改标签文本。我希望我已经解释了我的情况。对不起我的英语不好。你好,卢卡。

最佳答案

一种方法是,假设 inserimentoToolbar 在您的界面中声明:

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

这实际上仅在假设您的工具栏不变(就对象而言)的情况下才有效。

更理想的解决方案是将 lblTotCaratteri 放入您的界面中(在您的头文件中)。喜欢:

@interface UntitledViewController : UIViewController {
UILabel *lblTotCaratteri;
}

那么你包含的代码就会像

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

然后在任何时候,只需使用

lblTotCaratteri.text = @"sometext";

关于iphone - 以编程方式更改工具栏上 UILabel (UIBarButtonItem) 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1225457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com