UISlider 在 UIView 中时丢失目标操作

发布于 2024-10-09 16:33:29 字数 5082 浏览 6 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病女 2024-10-16 16:33:29

该框架是有问题的代码。我只需要使用它自己的框架来启动滑块,而不是使用传入的框架。

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

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.

float startX = frame.origin.x;
float startY = frame.origin.y;

slider = [[UISlider alloc] initWithFrame:frame];

needs to be

slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 22.0)];
黒涩兲箜 2024-10-16 16:33:29

您是否将新子类的 userInteractionEnabled 设置为 YES ?您的视图确定能覆盖滑块占据的整个区域吗?

Did you set userInteractionEnabled to YES for your new subclass? Is your view sure to cover the whole area your sliders take up?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文