gpt4 book ai didi

ios - 如何使用 slider 更改 UILabel

转载 作者:行者123 更新时间:2023-11-28 21:06:31 25 4
gpt4 key购买 nike

我是 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
}

@end

但是我收到一个错误提示

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

等等……

关于ios - 如何使用 slider 更改 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45472195/

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