如何修复从另一个类调用时处理PanGestureRecognizer的错误?

时间:2015-08-20 09:27:49

标签: ios objective-c iphone uiview

我想将PanGestureRecognizer添加到UIViewController的UIView中。我在9个相等的部分划分视图,并由他们创建一个UIViews。我还在每个部分都添加了PanGestureRocgnizer。当我在另一个需要它的类中调用它时它工作正常但是当我在某个部分触摸屏幕以启动PangestureRecognizer的句柄方法时,我得到错误: [UIView handlePanGestureRocognizer:]:无法识别的选择器发送到实例0x17de2ee0 即可。这是我的代码:

#import "CustomGestureRecognizer.h"

@implementation CustomGestureRecognizer
@synthesize arrayOfPatternsForComparison;
@synthesize arrayOfSubviews;
@synthesize arrayOfPanGestureRecognizers;



- (void)initialize:(UIView *)view {
    arrayOfPatternsForComparison = [[NSMutableArray alloc] init];
    arrayOfSubviews = [[NSMutableArray alloc] init];
    arrayOfPanGestureRecognizers = [[NSMutableArray alloc] init];

    [self createPatternsForComparison];
    [self splitScreenInParts:view];
    [self setPanGestrueRecognizersOnEveryPartOfTheScreen];
}

- (void)createPatternsForComparison {

}

- (void)splitScreenInParts:(UIView *)view {
    CGSize onePart = CGSizeMake(view.frame.size.width/3, view.frame.size.height/3);
    CGFloat x_positionOfPart = 0;
    CGFloat y_positionOfPart = 0;

    for (NSUInteger j=0; j<3; j++) {
        for (NSUInteger i=0; i<3; i++) {
            UIView *_view = [[UIView alloc] initWithFrame:CGRectMake(x_positionOfPart, y_positionOfPart, onePart.width, onePart.height)];

            [[_view layer] setBorderWidth:1.0];
            [[_view layer] setBorderColor:[UIColor redColor].CGColor];

            x_positionOfPart += onePart.width;
            [arrayOfSubviews addObject:_view];
            [view addSubview:_view];
        }
        y_positionOfPart += onePart.height;
        x_positionOfPart = 0;
    }

}

- (void)setPanGestrueRecognizersOnEveryPartOfTheScreen {
    for (UIView *view in arrayOfSubviews) {
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];
        [[panGestureRecognizer view] setTag:[arrayOfSubviews indexOfObject:view]];
        [view addGestureRecognizer:panGestureRecognizer];
    }
}


- (void)handlePanGestureRocognizer:(UIPanGestureRecognizer *)sender {
    NSLog(@"%d", [[sender view] tag]);
}



@end

在另一个UIViewController类中,我这样调用它:

- (void)viewWillAppear:(BOOL)animated {
    CustomGestureRecognizer *cgr = [[CustomGestureRecognizer alloc] init];
    [cgr initialize:[self view]];
}

如何解决这个问题?谢谢你的回答。

3 个答案:

答案 0 :(得分:0)

因为调用它时会释放cgr

您必须在.h中创建var CustomGestureRecognizer *cgr,然后:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    cgr = [[CustomGestureRecognizer alloc] init];
    [cgr initialize:[self view]];
}

然后更改此行

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestureRocognizer:)];

答案 1 :(得分:0)

问题出在这条线上。

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];

在这里,您将目标作为“视图”传递,这是一个子视图。目标应该是一个viewController对象,如“self”或任何其他viewController对象。

此外,您只需创建9个视图(我认为在NSObject子类中)并添加到数组中,但这些视图不会作为子视图添加到任何视图中。为此,您可以将该数组返回到viewController,并将self手势添加到以self为目标的视图中。然后它会工作。

答案 2 :(得分:0)

我通过在另一个类的 .h 中添加var * CustomGestureRecognizer cgr 来解决这个问题,例如anhtu和改变

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];

目标指向 自我 ,如Dev所说。