我正在尝试创建一个UISliders页面。滑块的数量是动态的。
我有一个PageViewController类,当我将UISliders放在PageViewController的子视图中时,滑块可以工作。
我想将滑块封装到UIView子类中,这样我就可以保存它们的状态/更改耦合标签等。但是当我这样做时,滑块停止工作。我已经尝试了一些事情,而且我认为我可能只是缺少一个基本的理解,希望有人可以指出。
这适用于(来自PageViewController.m):
if ([[controlData type] isEqualToString:@"SliderInt"])
{
NSDictionary *data = controlData.data;
NSLog(@"build SliderInt %@ %@ %@", [controlData.data objectForKey:@"min"], [controlData.data objectForKey:@"max"], [controlData.data objectForKey:@"value"]);
CGRect frame = CGRectMake(startX, startY, 200.0, 22.00);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
CGRect labelFrame = CGRectMake(startX, startY+22.0, 100.0, 30.0);
UILabel *minLabel = [[UILabel alloc] initWithFrame:labelFrame];
[minLabel setFont:[UIFont systemFontOfSize:12]];
minLabel.textAlignment = UITextAlignmentLeft;
[minLabel setText:@"minLabel:"];
labelFrame = CGRectMake(startX+200.0, startY+22.0, 100.0, 30.0);
UILabel *maxLabel = [[UILabel alloc] initWithFrame:labelFrame];
[maxLabel setFont:[UIFont systemFontOfSize:12]];
maxLabel.textAlignment = UITextAlignmentLeft;
[maxLabel setText:@"maxLabel:"];
labelFrame = CGRectMake(startX+220.0, startY+0.0, 100.0, 30.0);
UILabel *valueLabel = [[UILabel alloc] initWithFrame:labelFrame];
[valueLabel setFont:[UIFont systemFontOfSize:12]];
valueLabel.textAlignment = UITextAlignmentLeft;
[valueLabel setText:@"valueLabel:"];
minLabel.text = [data objectForKey:@"min"];
maxLabel.text = [data objectForKey:@"max"];
valueLabel.text = [data objectForKey:@"value"];
slider.minimumValue = [[data objectForKey:@"min"] intValue];
slider.maximumValue = [[data objectForKey:@"max"] intValue];
slider.value = [[data objectForKey:@"value"] intValue];
//
[contentView addSubview:slider];
[contentView addSubview:minLabel];
[contentView addSubview:maxLabel];
[contentView addSubview:valueLabel];
[slider release];
[minLabel release];
[maxLabel release];
[valueLabel release];
startY+=60;
}
if ([[controlData type] isEqualToString:@"SliderFloat"])
{
NSLog(@"build SliderFloat");
}
然而,将其移入此课程会打破它
@interface SliderIntView : UIView {
UILabel *minLabel;
UILabel *maxLabel;
UILabel *valueLabel;
UISlider *slider;
NSDictionary *data;
}
@property(nonatomic, retain) UISlider *slider;
@property(nonatomic, retain) UILabel *minLabel;
@property(nonatomic, retain) UILabel *maxLabel;
@property(nonatomic, retain) UILabel *valueLabel;
@property(nonatomic, retain) NSDictionary *data;
- (id)initWithFrame:(CGRect)frame withData:(NSDictionary *)controlData;
-(void) sliderAction:(id) sender;
@end
实施
#import "SliderIntView.h"
@implementation SliderIntView
@synthesize slider, minLabel, maxLabel, valueLabel, data;
- (id)initWithFrame:(CGRect)frame withData:(NSDictionary *) controlData {
self = [super initWithFrame:frame];
if (self) {
self.data = controlData;
float startX = frame.origin.x;
float startY = frame.origin.y;
slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
CGRect labelFrame = CGRectMake(startX, startY+22.0, 100.0, 30.0);
minLabel = [[UILabel alloc] initWithFrame:labelFrame];
[minLabel setFont:[UIFont systemFontOfSize:12]];
minLabel.textAlignment = UITextAlignmentLeft;
[minLabel setText:@"minLabel:"];
labelFrame = CGRectMake(startX+200.0, startY+22.0, 100.0, 30.0);
maxLabel = [[UILabel alloc] initWithFrame:labelFrame];
[maxLabel setFont:[UIFont systemFontOfSize:12]];
maxLabel.textAlignment = UITextAlignmentLeft;
[maxLabel setText:@"maxLabel:"];
labelFrame = CGRectMake(startX+220.0, startY+0.0, 100.0, 30.0);
valueLabel = [[UILabel alloc] initWithFrame:labelFrame];
[valueLabel setFont:[UIFont systemFontOfSize:12]];
valueLabel.textAlignment = UITextAlignmentLeft;
[valueLabel setText:@"valueLabel:"];
minLabel.text = [data objectForKey:@"min"];
maxLabel.text = [data objectForKey:@"max"];
valueLabel.text = [data objectForKey:@"value"];
slider.minimumValue = [[data objectForKey:@"min"] intValue];
slider.maximumValue = [[data objectForKey:@"max"] intValue];
slider.value = [[data objectForKey:@"value"] intValue];
[self addSubview:slider];
//[self addSubview:maxLabel];
//[self addSubview:minLabel];
//[self addSubview:valueLabel];
}
return self;
}
-(void) sliderAction:(id) sender
{
NSLog("@never happens");
}
答案 0 :(得分:0)
您为新的子类设置了userInteractionEnabled
到YES
了吗?您的视图是否肯定会覆盖滑块占据的整个区域?
答案 1 :(得分:0)
框架是违规代码。我只需要使用自己的框架启动滑块,而不是使用传入的滑块。
float startX = frame.origin.x;
float startY = frame.origin.y;
slider = [[UISlider alloc] initWithFrame:frame];
需要
slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 22.0)];