gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:25:21 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com