ios - 以编程方式创建并添加到 uiimageview 时,UIButton 无响应

标签 ios uiview uibutton

我的 viewDidLoad 方法中有以下代码。这是一个带有 ScrollView 和页面 Controller 的 uiviewcontroller。

我有以下代码,它从数组创建 View ,在第 3 页放置一个按钮:

 for (int i = 0; i < imageNames.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;

UIView *subview = [[UIView alloc] initWithFrame:frame];

UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];

myImageView.image=[UIImage imageNamed:[imageNames objectAtIndex:i]];

[subview addSubview: myImageView];


UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button addTarget:self 
           action:@selector(doSomething:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];

if(i == 1){
[myImageView addSubview:button];
}
[self.scrollView addSubview:subview];

我有一个 ibaction 将关闭此 View ,因为它是模态视图: -(IBAction)doSomething:(id)发件人{ [ self dismissModalViewControllerAnimated:YES];

问题是这个按钮没有反应。它甚至不会在触摸时突出显示,更不用说调用 doSomething 方法了。

我做错了什么?

最佳答案

请注意确定这是否是问题所在,但很有可能。

来自UIImageView文档:

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

关于ios - 以编程方式创建并添加到 uiimageview 时,UIButton 无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11275780/

相关文章:

ios - 对于这种情况,我应该使用 Google 应用内购买还是 Google Pay?

iphone - 使用 UITabBarController 添加持久化 UIView

ios - UIButton 在 UIImageView 下不可点击(swift)

ios - 在 UIButton 之间滑动

objective-c - 如何在 NSMutableArray 中存储 CGContext 状态?

ios - 以下代码中的错误是什么?

objective-c - IOS - 动画没有响应

uiview - UIView 边框中的文本

ios - 获取应用程序的所有 View - 每时每刻?

iphone - 每次点击 4 个按钮标题更改