UISlider 在 UIView 中时丢失目标操作
我正在尝试创建一个 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");
}
I am trying to create a page of UISliders. The amount of sliders is dynamic.
I have a PageViewController class and when I put the UISliders in a subview of PageViewController the sliders work.
I would like to encapsulate the sliders into a UIView subclass so I can save their state/change coupled labels, etc. However when I do it the sliders stop working. I've tried a few things and am at the point to where I think I may just be missing a fundamental understanding that hopefully someone can point out.
This works (from 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");
}
However moving it into this class breaks it
@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
The implementation
#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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该框架是有问题的代码。我只需要使用它自己的框架来启动滑块,而不是使用传入的框架。
需要
The frame was the offending code. I just needed to start the slider with it's own frame as opposed to using the one passed in.
needs to be
您是否将新子类的
userInteractionEnabled
设置为YES
?您的视图确定能覆盖滑块占据的整个区域吗?Did you set
userInteractionEnabled
toYES
for your new subclass? Is your view sure to cover the whole area your sliders take up?