gpt4 book ai didi

ios - 自包含 UIView 子类中的 UITapGestureRecognizer EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-29 13:07:02 24 4
gpt4 key购买 nike

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

我正在创建这样的 View :

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
}

最佳答案

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

关于ios - 自包含 UIView 子类中的 UITapGestureRecognizer EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18388356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com