【问题标题】:UIButton in UIView not getting touchesUIView 中的 UIButton 没有被触摸
【发布时间】:2023-04-03 15:18:01
【问题描述】:

我在 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 的子视图返回这一事实是否重要?

【问题讨论】:

  • question_middle_view的框架是什么?可能太小了。
  • 它根据填充它的内容的高度进行缩放。可悲的是,它工作正常。

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


【解决方案1】:

尝试在你的 current_imageView 和 parentView 上设置userInterActionEnabled

编辑:

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

【讨论】:

  • 这似乎没有任何改变。 :(
  • @user1048121 看看编辑后的答案,我刚刚注意到另一个问题。
  • 我确实定义了函数。这是标题行:code- (void)viewMedia:(UIButton*)sender;code 和函数:code-(void)viewMedia:(UIButton *)sender { NSLog(@"Sender Tag: %d", 发件人.tag); }code
【解决方案2】:

变化:

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

收件人:

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

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

【讨论】:

  • UIControlEventTouchDown 和 UIControlEventTouchUpInside 都试过了,一开始是 UIControlEventTouchUpInside,只是在贴代码的时候忘记改回来了。
猜你喜欢
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多