I have a UIButton inside a class. I want to set a target for the button like so.
[myController.dateButton addTarget:self action:@selector(showAction:) forControlEvents:UIControlEventTouchUpInside];
For some reason, the selector is never called when I press the button. I tried to debug the problem by doing the following but I get nil for the NSSet
NSSet *allTargets = [myController.dateButton allTargets];
Any suggestions 开发者_运维百科on what I may be doing wrong?
The selector is defined as follows:
- (void)showAction:(id)sender
{
// Do stuff
}
Maybe this be the solution, cz i dnt think there's anything wrong in your code.
When you declare your button as
UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
never have the release called on that btn.
[tempBtn release];//dont do this!!
I tried this my selves and it was working fine.
- (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");
}
精彩评论