2012-01-14 67 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