Objective-c – UIButton selector not called

event-listeneripadobjective cuibutton

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
}

Best Answer

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");
}