如何使用滑块更改UILabel

时间:2017-08-02 22:47:51

标签: ios objective-c uilabel uislider

我是目标c的新手,我想将标签文本更改为滑块的值。

我应该先添加一些文本吗?我应该在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
}

@end

但是我收到了一个错误

  

错误:使用未声明的标识符“sliderText”

5 个答案:

答案 0 :(得分:2)

在文件中全局定义Label对象。

@implementation slRootViewController {
    NSMutableArray *_objects; 
    UILabel *sliderText; //Define globally label object
}

并使用全局对象更改此标签的名称或任何活动,例如 更改名称,

sliderText.text = @"Set Name";

设置文字颜色

sliderText.textColor = [UIColor redColor];

依旧......

答案 1 :(得分:0)

错误是由变量的范围引起的。您在sliderText内声明loadView,最后在另一个名为sliderAction的方法中访问它。它应该是一个实例变量(根据您的需要在您的.h或.m文件中定义)。就像你使用_objects一样。

@implementation slRootViewController {
    NSMutableArray *_objects;
    UILabel *sliderText; // Move it to your .h file if needed
}

答案 2 :(得分:0)

应该是这样的

#import "slRootViewController.h"

@implementation slRootViewController {

    NSMutableArray *_objects;  

}

@property (strong ,nonatomic) UILabel *sliderText;

- (void)loadView {
    [super loadView];

    _objects = [[NSMutableArray alloc] init];

    self.title = @"Slider Test ";


    CGRect frame = CGRectMake(60, 200.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;

    self.sliderText = [[UILabel alloc] initWithFrame:CGRectMake(60,CGRectGetMaxY(slider.frame)+40,120,20)];
    //You can change y axis what you want
    [self.view addSubview:slider];
    [self.view addSubview:self.sliderText];
}

// voids
-(void)sliderAction:(id)sender
{
    UISlider *slider = (UISlider*)sender;
    self.sliderText.text=[NSString stringWithFormat:@"%f", slider.value];
    //-- Do further actions
}

答案 3 :(得分:0)

让我清楚你的错误。

当您尝试访问未声明的控制器或类中的任何对象,或者您在一个方法中声明并尝试调用其外部方法(称为超出范围)时,那么您会收到这样的错误。

<强>解决方案:

1)检查关于变量声明的头文件。

2)在这里你写 self.sliderText 。因此,请确保您正确合成它。

答案 4 :(得分:0)

您只需要更改实现部分中的UILabel声明..,如下面的代码

中所示
    #import "slRootViewController.h"
@implementation slRootViewController {
    NSMutableArray *_objects; 
  UILabel *sliderText; //<<---
}

- (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;

    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;
     sliderText.text=[NSString stringWithFormat:@"%f", slider.value];
}

@end