我是 objective-c 的新手,我想将标签文本更改为 slider 的值。
我应该先添加一些文本,还是应该在 Action 中添加 UILabel
?
我有的是:
#import "slRootViewController.h"
@implementation slRootViewController {
NSMutableArray *_objects;
}
- (void)loadView {
[super loadView];
_objects = [[NSMutableArray alloc] init];
self.title = @"Slider Test ";
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
UILabel *sliderText=[ [UILabel alloc] initWithFrame:CGRectMake(273,442,32,20)];
[self.view addSubview:slider];
[self.view addSubview:sliderText];
}
// voids
-(void)sliderAction:(id)sender
{
UISlider *slider = (UISlider*)sender;
self.sliderText.text=[NSString stringWithFormat:@"%f", slider.value];
//-- Do further actions
}
@kết thúc
但是我收到一个错误提示
error: Use of undeclared identifier "sliderText"
在您的文件中全局定义 Label 对象。
@implementation slRootViewController {
NSMutableArray *_objects;
UILabel *sliderText; //Define globally label object
}
并使用全局对象更改此标签的名称或任何事件,例如更改名称,
sliderText.text = @"Set Name";
设置文字颜色,
sliderText.textColor = [UIColor redColor];
vân vân……
Tôi là một lập trình viên xuất sắc, rất giỏi!