[英]How to set subview as a first responder?
我试图在单击关闭按钮时关闭图像视图。但是按钮不响应触摸事件。 这是我的代码:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:@"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}
为了使UIImageView处理触摸,您需要将userInteractionEnabled设置为YES,它默认为NO。 尝试:
[imageView setUserInteractionEnabled:YES];
UIImageView userInteractionEnabled
有1个属性,其值默认为NO。 所以在你的代码中添加
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;
也许在您的self.view中还有另一个带有tag = 1的视图。 请尝试编辑这样单击的按钮:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.