简体   繁体   English

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

[英]Adding addTarget: method to custom UIView

I created a custom UIView something like UIStepper, because I don't like the appearance of the UIStepper. 我创建了一个类似于UIStepper的自定义UIView,因为我不喜欢UIStepper的外观。 I want UIStepper have a personal count label so I added it to my custom view and created a method to increase-decrease it. 我希望UIStepper具有个人计数标签,所以我将其添加到自定义视图中并创建了一种增加或减少它的方法。 Now I need a [customView addTarget:(id) action:(del) forControlEvents:(UIControlEvents)] method to catch UIControlEventValueChanged ; 现在,我需要一个[customView addTarget:(id)action:(del)forControlEvents:(UIControlEvents)]方法来捕获UIControlEventValueChanged but I couldn't find anything about implementing it. 但我找不到实现它的任何方法。 I dig around the UIButton.h, UIStepper.h files but no luck too.. Can anyone help me to do that? 我在UIButton.h,UIStepper.h文件中进行挖掘,但是也没有运气。有人可以帮助我做到这一点吗?

Here is how I created the custom view... 这是我创建自定义视图的方式...

CounterView.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 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

Just allow the actions to be set from outside of your control. 只需允许从控件外部设置操作即可。 Here is how to do it: 这是操作方法:

@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

Then use methods: 然后使用方法:

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

to set a target and actions for increasing and decreasing the values. 设定增加和减少值的目标和措施。

in the ViewController where you instantiate your CounterView you add this 在实例化CounterView的ViewController中,添加此代码

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

and the you implement the call back method: 并且您实现了回调方法:

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

  //Do stuff here...
}

i hope that help you ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM