1

我创建了一个类似于 UIStepper 的自定义 UIView,因为我不喜欢 UIStepper 的外观。我希望 UIStepper 有一个个人计数标签,所以我将它添加到我的自定义视图中并创建了一种增加-减少它的方法。现在我需要一个[customView addTarget:(id) action:(del) forControlEvents:(UIControlEvents)]方法来捕获UIControlEventValueChanged;但我找不到任何关于实施它的信息。我挖掘了 UIButton.h、UIStepper.h 文件,但也没有运气.. 谁能帮我做到这一点?

这是我创建自定义视图的方式...

CounterView.h

#import <UIKit/UIKit.h>

@interface CounterView : UIView
@property NSString* name;
@property NSInteger count;
@property UILabel *label;
@property NSInteger customTag;

- (id)initWithX:(CGFloat)xPoint WithY:(CGFloat)yPoint WithName:(NSString*)newName withCount:(NSInteger)newCount withCustomTag:(NSInteger)newTag;
@end

CounterView.m

@implementation CounterView

- (id)initWithX:(CGFloat)xPoint WithY:(CGFloat)yPoint WithName:(NSString*)newName withCount:(NSInteger)newCount withCustomTag:(NSInteger)newTag
{
self = [super initWithFrame:CGRectMake(xPoint, yPoint, 24, 52)];
if (self) {
    self.customTag = newTag;
    self.count = newCount;
    self.name = newName;

    UIButton *btnUp = [[UIButton alloc] initWithFrame:CGRectMake(3, 2, 18, 12)];
    [btnUp setImage:[UIImage imageNamed:@"top.png"] forState:UIControlStateNormal];
    [btnUp addTarget:self action:@selector(increaseValue) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btnDown = [[UIButton alloc] initWithFrame:CGRectMake(3, 38, 18, 12)];
    [btnDown setImage:[UIImage imageNamed:@"bottom.png"] forState:UIControlStateNormal];
    [btnDown addTarget:self action:@selector(decreaseValue) forControlEvents:UIControlEventTouchUpInside];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 24, 24)];
    [self.label setText:[NSString stringWithFormat:@"%ld", (long)self.count]];
    self.label.textAlignment = NSTextAlignmentCenter;

    [self addSubview:btnUp];
    [self addSubview:btnDown];
    [self addSubview:self.label];
}
return self;
}

-(void) increaseValue{
self.count++;
[self.label setText:[NSString stringWithFormat:@"%ld", (long)self.count]];
}
-(void) decreaseValue{
self.count--;
[self.label setText:[NSString stringWithFormat:@"%ld", (long)self.count]];
}
@end
4

2 回答 2

2

只需允许从您的控制之外设置操作。这是如何做到的:

@interface CounterView : UIView
...
@property(nonatomic, strong) UIButton *btnUp;
@property(nonatomic, strong) UIButton *btnDown;

- (void)addUpTarget:(id)target action:(SEL)action;
- (void)addDownTarget:(id)target action:(SEL)action;

@end

@implementation CounterView

...

- (void)addUpTarget:(id)target action:(SEL)action
{
    [_btnUp addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)addDownTarget:(id)target action:(SEL)action
{
    [_btnDonw addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

...

@end

然后使用方法:

- (void)addUpTarget:(id)target action:(SEL)action;
- (void)addDonwTarget:(id)target action:(SEL)action;

设定增加和减少值的目标和行动。

于 2014-03-05T20:55:19.467 回答
2

在您实例化 CounterView 的 ViewController 中,添加此

UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[yourViewInstantiation addGestureRecognizer:singleFingerTap];
[singleFingerTap release];

并且您实现了回调方法:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

  //Do stuff here...
}

我希望对你有帮助!

于 2014-03-05T21:00:24.337 回答