Alternative UIButton addTarget: action: forControlEvents: method in IOS
I know this question has been asked so many times, but I want to pass a custom object as an argument when the button is clicked.
UIButton addTarget:action:forControlEvents:
does not allow us to do this, but it is important to me, so I can do further based on custom objects.
If an alternative for adding a target is possible, please give a solution.
The code looks like this:
Custom Object:
HeaderData *cell ;
Button
_forward =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[_forward setTitle:@"F" forState:UIControlStateNormal];
_forward.frame = CGRectMake(300, (_CELL_HEIGHT-_LABEL_HEIGHT)/2, 10 ,_LABEL_HEIGHT);]
[_forward addTarget:web action:@selector(functionName:) forControlEvents:UIControlEventTouchUpInside];
and the function called when clicking the UIButton _forward is:
-(void)functionName:(UIButton *)sender{
//code for further programming on the basis of cell.
}
+3
source to share
1 answer
You can create your own UIButton subclass:
@interface CustomDataButton : UIButton
@property (nonatomic, strong) id userData;
@end
Then in functionName you can pull the data:
-(void)functionName:(UIButton *)sender
{
if ([sender isKindOfClass:[CustomDataButton class]])
{
id customData = ((CustomDataButton *) sender).userData;
}
}
caveat : written without IDE. Beware of typos, etc.
+3
source to share