简体   繁体   中英

Alternative option of UIButton addTarget:action:forControlEvents: method in IOS

I know that this question has been asked so many times but i want to pass a custom object as an argument on clicking a button.

UIButton addTarget:action:forControlEvents: doesn't allow us to do that but it is important for me so I can do further on the basis of custom objects.

If an alternative for add target is possible, so please give the solution.

The code is 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 which is called on clicking the UIButton _forward is:

-(void)functionName:(UIButton *)sender{
    //code for further programming on the basis of cell.
}

You could make a custom subclass of UIButton:

@interface CustomDataButton : UIButton
@property (nonatomic, strong) id userData;
@end

Then in functionName, you can pull the data back out:

-(void)functionName:(UIButton *)sender 
{
    if ([sender isKindOfClass:[CustomDataButton class]])
    {
        id customData = ((CustomDataButton *) sender).userData;
    }
}

caveat : written without an IDE. Watch out for typos etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM