将addTarget:方法添加到自定义UIView

时间:2014-03-05 20:52:41

标签: ios objective-c uiview custom-view uicontrolevents

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

2 个答案:

答案 0 :(得分: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;

设置目标以及增加和减少值的操作。

答案 1 :(得分:2)

在ViewController中,您实例化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...
}

我希望能帮到你!