UIVut的UIButton没有接触到

时间:2011-11-15 18:30:26

标签: iphone objective-c cocoa-touch uibutton singleton

我在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的子视图吗?

2 个答案:

答案 0 :(得分:1)

尝试在current_imageView和parentView上设置userInterActionEnabled

编辑:

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

答案 1 :(得分:0)

更改:

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

要:

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

UIControlEventTouchUpInside 是检测UIButton触摸的正确控制事件。