iOS - passing arguments in action:@selector()

匆匆过客 提交于 2019-11-29 08:19:10

Why don't you make a Custom UIButton class and have the object as property?

See below.

"MyButton.h"

@interface MyButton : UIButton
@property(nonatomic, strong)MyClass *obj;
@end

"MyButton.m"

#import "MyButton.h"

@implementation MyButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

Now assign MyButton class to your actual button in the cell/ or initialize the custom button instead of normal UIButton class and assign the object directly.

And in your IBAction where sender=MyButton

- (void) quantityDown:(id)sender{
   MyButton *btn  = (MyButton *)sender;
   //You get the object directly
   btn.obj;
}

Doing it this way you can actually access as many properties you want easily. And its useful in other implementations too.

Hope it helps.

Charan Giri

Button can carry only one input so keep your sender and rowNum same so that it can be handled easily In cell for row method.

UIButton *b = [UIButton buttonWithType:UIButtonTypeContactAdd];
b.tag = indexPath.row;
[b addTarget:self action:@selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];

Your method

 - (void)quantityDown:(id)sender  
    {    
       NSLog(@"%d", sender.tag);  
    }

hope this will help...

Rashad

Set each button tag as indexPath.row. Then just declare the function:

- (void)quantityDown:(id)sender

In that method do this:

UIButton *btn = (UIButton *)sender;

Add target like this:

[buttonDown addTarget:self action:@selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];

From btn.tag you can get the row number. Hope this helps. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!