Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将addTarget:方法添加到自定义UIView_Ios_Objective C_Uiview_Custom View_Uicontrolevents - Fatal编程技术网

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

Ios 将addTarget:方法添加到自定义UIView,ios,objective-c,uiview,custom-view,uicontrolevents,Ios,Objective C,Uiview,Custom View,Uicontrolevents,我创建了一个自定义UIView,类似于UIStepper,因为我不喜欢UIStepper的外观。我希望UIStepper有一个个人计数标签,所以我将其添加到自定义视图中,并创建了一个方法来增加或减少它。现在我需要一个[customView addTarget:(id)action:(del)forControlEvents:(UIControlEvents)]方法来捕获UIControlEventValueChanged;但是我找不到任何关于实现它的东西。我在UIButton.h、UIStepp

我创建了一个自定义UIView,类似于UIStepper,因为我不喜欢UIStepper的外观。我希望UIStepper有一个个人计数标签,所以我将其添加到自定义视图中,并创建了一个方法来增加或减少它。现在我需要一个[customView addTarget:(id)action:(del)forControlEvents:(UIControlEvents)]方法来捕获UIControlEventValueChanged;但是我找不到任何关于实现它的东西。我在UIButton.h、UIStepper.h文件周围搜索,但也没有运气。。有人能帮我吗

下面是我如何创建自定义视图

反视图.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

只需允许在您的控制之外设置操作。以下是如何做到这一点:

@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;

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

只需允许在您的控制之外设置操作即可。以下是如何做到这一点:

@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;

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

在实例化计数器视图的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...
}

我希望这对你有帮助

在实例化计数器视图的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...
}

我希望这对你有帮助

但我有两个UIButton作为子视图。我给了他们UIControlEventTouchUpInside来更新UILabel的文本。我必须用手势识别器以你的方式重新执行按钮点击?好的。。我试试看。对不起,我没听清你的问题。我更新了我的答案。没问题。。也许是因为我的英语。。谢谢你的帮助,我会试试的。但添加addTarget方法的方法会更好。但可能没有办法做到这一点。。我还不知道=),但我有两个UIButton作为子视图。我给了他们UIControlEventTouchUpInside来更新UILabel的文本。我必须用手势识别器以你的方式重新执行按钮点击?好的。。我试试看。对不起,我没听清你的问题。我更新了我的答案。没问题。。也许是因为我的英语。。谢谢你的帮助,我会试试的。但添加addTarget方法的方法会更好。但可能没有办法做到这一点。。我还不知道=)你这么说,不要删除UIButtons的点击,而是在它们上面添加一个手势识别器。。因此,通过按钮单击(UIControlEventTouchUpInside),视图将更新其标签。使用手势识别器,视图将向视图控制器显示“我已被单击”。。我说得对吗?没错,但我看到你有两个按钮!!最好的方法是实现NSNotificationNSNotification。。。隐马尔可夫模型。。我还没用过它。。我也会试试看。谢谢。如果你这么说,不要删除UIButtons的点击,而是在其上添加一个手势识别器。。因此,通过按钮单击(UIControlEventTouchUpInside),视图将更新其标签。使用手势识别器,视图将向视图控制器显示“我已被单击”。。我说得对吗?没错,但我看到你有两个按钮!!最好的方法是实现NSNotificationNSNotification。。。隐马尔可夫模型。。我还没用过它。。我也会试试看。谢谢