1

我已经多次使用 UITapGestureRecognizer 了,但在这种情况下,当点击发生时我会得到 EXC_BAD_ACCESS。我认为这与我将其添加到警报类型覆盖视图的事实有关。并且由于某种原因,即使它在屏幕上,也不会保留覆盖视图。

我正在创建这样的视图:

HelperView *testHelper = [HelperView helperWithBodyText:@"test text"];
[testHelper presentAtPoint:screenCenter];

HelperView.m 中的便捷方法如下所示:

+ (id)helperWithBodyText:(NSString*)text
{
    return [[self alloc] initWithFrame:CGRectZero bodyText:text];
}

其余代码如下所示:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.container = [[AGWindowView alloc] initAndAddToKeyWindow];
        self.container.supportedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;

        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        self.overlay.backgroundColor = [UIColor redColor];
        self.overlay.alpha = 0.6;
        [self.container addSubviewAndFillBounds:self.overlay]; //this fills the screen with a transparent red color, for testing

        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissMe:)];
        [self.overlay addGestureRecognizer:tap]; //I want to dismiss the whole view if the overlay is tapped

        self.content = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame bodyText:(NSString*)bodyText
{
    self = [self initWithFrame:frame];
    if (self) {

        //TEST frame
        self.content.frame = CGRectMake(0, 0, 217, 134);

        // Initialization code
        UIImage *bgImage = [[UIImage imageNamed:@"helper-bg"]
                                resizableImageWithCapInsets:UIEdgeInsetsMake(30, 28, 20, 20)];
        UIImageView *bgImgView = [[UIImageView alloc] initWithImage:bgImage];
        bgImgView.bounds = self.content.frame;
        [self.content addSubview:bgImgView];

}
    return self;
}

- (void)presentAtPoint:(CGPoint)loc
{
    CGPoint newPoint = [self.container convertPoint:loc toView:self.container];

    self.content.center = newPoint;
    [self.container addSubview:self.content];

}


- (void)dismissMe:(UITapGestureRecognizer*)recognizer
{
    //this never happens - I get EXC_BAD_ACCESS when I tap the overlay
}
4

1 回答 1

1

HelperView 不是显示的视图,也没有保留,但您正在用作手势识别器的目标。AGWindowView 属性“容器”正在由其超级视图显示和保留。您的代码需要重构,因为您有这个视图 HelperView,它本身不会显示任何内容,但是如果您希望它像这样工作,您需要保留 HelperView,这样它就不会自动释放。您可以通过将其分配给一个强实例变量来做到这一点。

于 2013-08-22T18:51:50.563 回答