我创建了一个类似于 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