2011-08-26 53 views
1

我有一個類內的UIButton。我想爲此按鈕設置一個目標。UIButton選擇器不叫

[myController.dateButton addTarget:self action:@selector(showAction:) forControlEvents:UIControlEventTouchUpInside]; 

由於某種原因,當我按下按鈕時從不會調用選擇器。我試着調試通過做題下面,但我得到零對我可能是做錯了的NSSet中

NSSet *allTargets = [myController.dateButton allTargets]; 

有什麼建議?

選擇器的定義如下:

- (void)showAction:(id)sender 
{ 
    // Do stuff 
} 

回答

4

也許這是解決方案,CZ我天璣認爲有什麼錯在你的代碼。

當你宣佈你的按鈕

UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

從來沒有所謂的對BTN釋放。

[tempBtn release];//dont do this!! 

我試過這我自己,它工作正常。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    tempBtn.frame = CGRectMake(50, 50, 50, 50); 
    [tempBtn addTarget:self action:@selector(hello:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:tempBtn]; 

} 

- (void) hello:(id) sender 
{ 
    NSLog(@"helloooo"); 
} 
+0

是的,它是autoreleased。 – 2011-08-26 14:33:27

+0

它第一次工作正常,但不是當這個東西在tableview單元格中,並且第二次綁定...!?! –