为什么在UIImageView上添加button以后点击button不能执行

谁抢名字啊 2013-07-10 01:17:24
我在学习UI控件的使用
今天想实现在UIImageView 上面添加一个UIButton,点击button以后输出一段话
但是我加上去以后不能点击button。
不知道是哪里写错了
我是初学者,可能问题很菜。求大神指点下啊!
我把截图跟代码都贴出来啊。




#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor yellowColor];

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 140, 160, 80)];
//imgView.highlighted = NO;
imgView.backgroundColor = [UIColor greenColor];
imgView.image = [UIImage imageNamed:@"BB"];
[self.window addSubview:imgView];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(80, 40, 80, 40);
// btn.selected = YES;
[btn setTitle:@"点我" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:btn];
// btn.enabled = YES;
[self.window makeKeyAndVisible];
return YES;
}

-(void)touchUpInside
{
NSLog(@"touchUpInside");
}
...全文
281 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
谁抢名字啊 2013-07-10
  • 打赏
  • 举报
回复
引用 2 楼 ningxin_chen 的回复:
因为UIImageView 默认是不接受Touch事件的 所以只要加上一句imgView.userInteractionEnabled = YES;
按照你说的方法就可以了 谢谢啊!
ningxin_chen 2013-07-10
  • 打赏
  • 举报
回复
因为UIImageView 默认是不接受Touch事件的 所以只要加上一句imgView.userInteractionEnabled = YES;
swne527 2013-07-10
  • 打赏
  • 举报
回复
路过,希望找到高手。

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧