2011-08-26 45 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单元格中,并且第二次绑定...!?! –