Jeremy
Jeremy

Reputation: 883

iPhone: how to detect when user clicks a button in a customized UITableViewCell

I have created a customized call that is called in UITableViewCell (cellForRowAtIndexPath). Everything worked great, but then I added a button (UIButton) to the cell. The button's "Referencing Outlet" is the custom cell.

I cannot work out how to read when a user clicks the button. Do I need to declare a delegate method in the header file containing the table or the customized cell header?

I tried the - (void)buttonPressedAction:(id)sender {} method, but it doesn't seem to be detecting when the button is pressed

I also tried:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSSet *aTouch = [event allTouches];
UITouch *touch = [aTouch anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.dashboardTable];
NSIndexPath *indexPath = [self.dashboardTable indexPathForRowAtPoint: currentTouchPosition];

NSLog(@"indexPath %@",indexPath);

}

but again, nothing registers when the button is clicked. I tried adding UIGestureRecognizerDelegate to .h but no luck.

Any help is appreciated.

Upvotes: 0

Views: 265

Answers (1)

sosborn
sosborn

Reputation: 14694

You need to set the action of the button. If you do this is a XIB all you have to do is control click the button and then drag to the controller. This should give you a list of actions available to the button.

To do this in code you would use the - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents method.

[myButton addTarget:myTargetController action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 1

Related Questions