通过委托在另一个类中调用手势识别器的选择器

时间:2014-06-28 12:46:36

标签: ios objective-c

我有一个自定义视图,它使用调用方法handleSingleTap的手势识别器。如果手势识别器和它调用的选择器的设置在同一个自定义视图(CircleView)中,这可以正常工作。但是,我需要viewController中的handleSingleTap方法,所以我(1)在自定义视图(CircleView),(2)在视图中创建了委托属性,当我(3)创建了手势识别器时,我为self.delegate的目标设置了handleSingleTap(而不仅仅是{{} 1}}就像最初一样)。在viewController.h中,(4)我说它符合协议并声明了方法self。在视图控制器的.m文件中,我为自定义视图(CircleView)创建了一个属性,然后在.m文件的viewDidLoad中,将handleSingleTap设置为自定义视图的委托。但是,当我点击自定义视图中的圆圈时,self方法永远不会在viewController中被调用。

你能解释为什么没有在viewController中调用选择器handleSingleTap吗?

1)创建协议

handleSingleTap

2)创建了委托属性

@protocol CircleViewDelegate <NSObject>
-(void)handleSingleTap:(UITapGestureRecognizer *) tapper;

@end
在CircleView中

3)创建了具有目标@property (nonatomic, weak) id <CircleViewDelegate> delegate;

的手势识别器
self.delegate

4)在viewController.h中,

-(void)setUpGestureRecognizers{

    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self.delegate action:@selector(handleSingleTap:)];
    singleFingerTap.numberOfTapsRequired=1;
    [self addGestureRecognizer:singleFingerTap]; 
}
viewController.m中的

   @interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, CircleViewDelegate>
    -(void)handleSingleTap:(UITapGestureRecognizer *) tapper;

viewDidLoad中

@property (strong, nonatomic) CircleView *overlay;

viewController中的此方法未被调用

self.overlay.delegate = self;

1 个答案:

答案 0 :(得分:1)

这是一个适合您的解决方案。当然你也可以用选择器来做,但这也肯定会起作用。

·H

#import <UIKit/UIKit.h>

@protocol CircleViewDelegate <NSObject>

@required
- (void) circleViewPressedByOneTap;

@end

@interface CircleView : UIView

@property (nonatomic,assign) id<CircleViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame andWithDelegate:(id<CircleViewDelegate>)del;

@end

的.m:

#import "CircleView.h"

@interface CircleView () <UIGestureRecognizerDelegate>

@end

@implementation CircleView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame andWithDelegate:(id<CircleViewDelegate>)del
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        delegate = del;
        [self setUpGestureRecognizers];
    }
    return self;
}

-(void)setUpGestureRecognizers{

    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.numberOfTapsRequired=1;
    [self addGestureRecognizer:singleFingerTap];
}

-(void)handleSingleTap:(UITapGestureRecognizer *) tapper{
    [delegate circleViewPressedByOneTap];
}

在你的ViewController .m:

#import "YouClassViewController.h"
#import "CircleView.h"

@interface YouClassViewController () <CircleViewDelegate>

@end

@implementation YouClassViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CircleView* yourCircleView = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 50, 50) andWithDelegate:self];
    [self.view addSubview:yourCircleView];
}

- (void) circleViewPressedByOneTap{
    NSLog(@"Yeaah you pressed it ! :) ");
}