繁体   English   中英

UIView中的UIButton没有接触

[英]UIButton in UIView not getting touches

我在viewController中有以下代码:

BubbleChatSingleton *myBubble = [BubbleChatSingleton sharedBubbleChat];
UIView *myQuestionView;
myQuestionView = [myBubble createBubbleChat];
[question_middle_view addSubview:myQuestionView];

[myBubble createBubbleChat]包含以下代码:

UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(18 + (65 * (media_count % 4)), (5 + (media_rows * 65)), 60, 60)];

UIImageView *current_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
// Get the image
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath = [NSString stringWithFormat:@"%@/question_image_%@.png", [documentPaths objectAtIndex:0], [current_media objectForKey:@"id"]];
UIImage *mediaImage = [self imageByCropping:[[UIImage alloc] initWithContentsOfFile:imagePath] toRect:CGRectMake(0, 0, 60, 60)];
[current_imageView setImage:mediaImage];
[mediaImage release];
[parentView addSubview:current_imageView];
[current_imageView release];               

UIButton *overlay_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
overlay_button.tag = [[current_media objectForKey:@"id"] intValue];
overlay_button.frame = CGRectMake(0, 0, 60, 60);
[overlay_button addTarget:self action:@selector(viewMedia:) forControlEvents:UIControlEventTouchDown];
[parentView addSubview:overlay_button];

但是,overlay_button没有收到任何点击事件。 看起来根本不像我按下按钮。 (即,我们通常使用UIButtionTypeRoundedRect看不到视觉变化)

我已经仔细检查过,按钮上方是否没有其他视图透明,还有其他原因吗? 我从单例方法将按钮作为UIView的子视图返回的事实很重要吗?

尝试在current_imageView和parentView上设置userInterActionEnabled

编辑:

我还注意到,您正在为self添加目标,在您的情况下为BubbleChatSingleton ,而不是您所在的控制器。因此,该操作将在BubbleChatSingleton触发。 你那里有这样的方法吗?

更改:

[overlay_button addTarget:self action:@selector(viewMedia:) forControlEvents:UIControlEventTouchDown];

至:

[overlay_button addTarget:self action:@selector(viewMedia:) forControlEvents:UIControlEventTouchUpInside];

UIControlEventTouchUpInside是用于检测UIButton触摸的适当控件事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM