简体   繁体   中英

UIButton in UIView not getting touches

I have the following code in a viewController:

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

The [myBubble createBubbleChat] contains the following code:

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];

However the overlay_button is not receiving any of the click events. It doesn't even look like I pressed the button at all. (ie. not visual changes us usually see with a UIButtionTypeRoundedRect)

I have double checked that no other views are transparent over top of the button, could anything else cause this? Is the fact that I am return the button as a subview of a UIView from a singleton method matter?

Try to set userInterActionEnabled on your current_imageView and parentView.

EDIT:

I also noticed that you are adding target for self , which in you case is BubbleChatSingleton , but not the controller you are in. So the action will be triggered in BubbleChatSingleton . Do you have such method in there?

Change:

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

To:

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

UIControlEventTouchUpInside is the proper control event for detecting UIButton touches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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