2012-01-14 64 views
0
menuView= [[UIImageView alloc] initWithFrame:CGRectMake(0,411, 320,49)]; 

UIButton *test = [UIButton buttonWithType:UIButtonTypeCustom ]; 
test.frame = CGRectMake(0, 0, 80, 49); 

[test addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

[menuView addSubview:test]; 

[self.view addSubview:menuView]; 

我触摸里面(测试)按钮。但没有工作。为什么??UIButton Touch事件不起作用

+1

呢'self'实现一个名为'buttonPressed:'的方法? – 2012-01-14 08:41:36

回答

2

也许你混合buttonPressed:buttonPressed

如果实现像

- (void)buttonPressed 
{ 
} 

的方法需要使用

[test addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 

注意:在结肠注意。

但是这取决于你做了什么,你需要实现诸如

- (void)buttonPressed:(id)sender 
{ 
} 
+1

我想你的意思是说他需要在添加到self.view后释放menuView。 'buttonWithType:'返回一个自动释放对象,所以示例代码绝对不应该释放按钮(测试)。唯一的时候你应该用'alloc'或'copy'创建一个对象来调用'release'。 – jonkroll 2012-01-14 09:25:53

+0

@jonkroll你是对的,它是autoreleased!我没有注意到它,谢谢! – Kjuly 2012-01-14 09:29:02