[英]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.