可能重复:
按钮操作在 iphone 中不起作用?
我有一个应用程序,在该应用程序中,我添加了一个图像视图,而不是导航栏视图。像这样在视图中确实加载了方法。
curtainsView=[[UIImageView alloc]init];
curtainsView.image=[UIImage imageNamed:@"curtains.png"];
curtainsView.frame=CGRectMake(0,-44, 320, 60);
curtainsView.backgroundColor=[UIColor clearColor];
[self.view addSubview:curtainsView];
现在我想在该图像视图上添加一个按钮。我正在尝试用这个来实现
UIImage *button2Image = [UIImage imageNamed:@"scroll_down.png"];
UIButton *scroll = [UIButton buttonWithType:UIButtonTypeCustom];
//scroll = [[UIButton alloc] init];
//self.scroll.tag=100;
[scroll setImage:button2Image forState:UIControlStateNormal];
scroll.frame = CGRectMake(270,-30, 40,40);
[scroll addTarget:self action:@selector(backpressed) forControlEvents:UIControlEventTouchUpInside];
//[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
不工作。然后我也试过这个
UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btnDetail.frame = CGRectMake(260,-15,100,100);
[btnDetail addTarget:self action:@selector(backpressed) forControlEvents:UIControlEventAllTouchEvents];
[self.curtainsView addSubview:btnDetail];
[self.curtainsView bringSubviewToFront:btnDetail];
[self.curtainsView setUserInteractionEnabled:YES];
也不工作。如果它在该图像视图的一侧,则它不起作用,但是当我尝试添加带有框架的第二个代码时,CGRectMake(260,0,100,100)
它可以正常工作。但我需要该按钮位于该图像视图内。有人可以帮我吗?