[英]UIButton Selector method not working
I am creating a UIIimageView programtically than I create the UIView and in uiview I added two uibutton programtically but button selector method not working . 我要以编程方式创建UIIimageView而不是创建UIView,并且在uiview中以编程方式添加了两个uibutton,但是按钮选择器方法不起作用。 I tried many thing but nothing happened . 我尝试了很多事情,但没有任何反应。 Here is my code 这是我的代码
-(void) pagingFunc{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
//ScrollView Size and Contents
CGSize mxSize = CGSizeMake( [imgesArry count]*width , height-114);
[scrlView setContentSize : mxSize];
self.customView.translatesAutoresizingMaskIntoConstraints =YES;
self.customView.frame = CGRectMake(0, 0, mxSize.width, mxSize.height);
int incX = 0 ;
for( int i = 0; i < [imgesArry count]; i++)
{
PFObject *imageObject = [imgesArry objectAtIndex:i];
PFFile *file;
if (height == 480) {
file = [imageObject objectForKey:@"image4s"];
}
else{
file = [imageObject objectForKey:@"image6s"];
}
NSString * imgFile = [file url];
UIImageView* imagView = [[UIImageView alloc]initWithFrame : CGRectMake(incX,0,width ,height)];
imgView.userInteractionEnabled = YES;
[imagView sd_setImageWithURL:[NSURL URLWithString:imgFile] placeholderImage:[UIImage imageNamed:@"UserUpdationImageCell.png"]];
btnView = [[UIView alloc] initWithFrame:CGRectMake(incX, imagView.frame.size.height-60, width, 50)];
btnView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
UIButton *bckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[bckBtn addTarget:self
action:@selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[bckBtn setTitle:@"Back" forState:UIControlStateNormal];
bckBtn.frame = CGRectMake(0 ,0, 160.0, 40.0);
UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[downloadBtn addTarget:self
action:@selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[downloadBtn setTitle:@"Download" forState:UIControlStateNormal];
downloadBtn.frame = CGRectMake(160 ,0, 160.0, 40.0);
[btnView addSubview:bckBtn];
[btnView addSubview:downloadBtn];
[self.customView addSubview:imagView];
[self.customView addSubview:btnView];
incX+= width;
}
[scrlView setContentOffset:CGPointMake(width*selectedIndex, 0)];
}
May be you can set : self.customView.userInteractionEnabled=YES;
可以设置为: self.customView.userInteractionEnabled=YES;
I write a simple demo you describe and it works: 我编写了一个您描述的简单演示,它的工作原理是:
// imagView -> view1 -> bckBtn
UIImageView *imagView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
imagView.userInteractionEnabled = YES;
imagView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imagView];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
view1.backgroundColor = [UIColor yellowColor];
[imagView addSubview:view1];
UIButton *bckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
bckBtn.frame = CGRectMake(10, 10, 50, 50);
bckBtn.backgroundColor = [UIColor redColor];
[bckBtn addTarget:self
action:@selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[bckBtn setTitle:@"Back" forState:UIControlStateNormal];
[view1 addSubview:bckBtn];
-(void)aMethod{
NSLog(@"button click");
}
Hope it helps. 希望能帮助到你。
Well 1st thing, make sure you don't have any overlays over buttons. 第一件事,请确保按钮上没有任何覆盖。 Also you might try using GestureRecognizers. 您也可以尝试使用GestureRecognizers。
Here's a Swift snippet, I'm pretty sure it can be translated to obj-c 这是一个Swift代码段,我很确定它可以翻译成obj-c
let xButton = UIButton(frame: CGRect(x: 0, y: 0, width: 160, height: 40))
xButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(aMethod)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.