添加addTarget:方法来定制UIView
问题描述:
我创建了一个像UIStepper这样的自定义UIView,因为我不喜欢UIStepper的外观。我想UIStepper有个人计数标签,所以我将它添加到我的自定义视图中,并创建了一个增加 - 减少它的方法。现在我需要一个[customView addTarget:(id)action:(del)forControlEvents:(UIControlEvents)]捕捉方法UIControlEventValueChanged;但我找不到任何有关实施它的事情。我围绕UIButton.h,UIStepper.h文件进行了挖掘,但没有运气。谁能帮我做到这一点?添加addTarget:方法来定制UIView
这里是我创建的自定义视图...
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
答
只要允许动作被设置从你的控制之外。这里是如何做到这一点:
@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;
设定的目标和行动增加和减少的值。
在视图控制器
答
你实例您CounterView添加此
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...
}
我希望帮助你!
但我有2个UIButtons作为子视图。我给他们** UIControlEventTouchUpInside **来更新UILabel的文本。我必须用你的方式重新实现按钮点击与gestureRecognisers? –
行..我会试试看。 –
对不起,我没有得到你的问题。我更新了我的答案。 – RaffAl