-1

I know thare are many questions on this but i just search for the another way.

I have a Table View which include a BUTTON. When this button is press, i like to pass the Two Arguments in that method.

This two arguments are id & indexPath.row.

How can I do this?

Help me to solve this..

Thank you,

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
user2526811
  • 1,233
  • 4
  • 20
  • 54
  • Show your implemented code – Rohan Aug 09 '13 at 10:13
  • What Do you mean by... **Two Arguments in that method** ?? – Kumar KL Aug 09 '13 at 10:22
  • Why do you want to do so when you can get clicked button's `indexPath` simply. – TheTiger Aug 09 '13 at 10:38
  • check my answer here - [How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?](http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector-by-parameters-in-ios/11936294#11936294) – TheTiger Aug 09 '13 at 10:40

3 Answers3

1

1. No need to pass two arguments in button's @selector to get indexPath:. The button itself is enough.

I have already given an answer here - How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?

2. If you want to that just for your knowledge and another way to do so then here is it.

Make a custom class of UIButton.

MyButton.h

@interface MyButton : UIButton
@property (nonatomic, retain) NSIndexPath *indPath;
@end

MyButton.m

#import "MyButton.h"

@implementation MyButton
@synthesize indPath = _indPath;

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

And use this button instead of UIButton.

 MyButton *btn = [[MyButton alloc] initWithFrame:CGRectMake(60.0, 2.0, 200.0, 40.0)];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:[NSString stringWithFormat:@"Button-%d", indexPath.row] forState:UIControlStateNormal];

// Assign indexPath here
btn.indPath = indexPath;

[btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:btn];

Note: Don't use UIButtonButtonWithType here.

And this is the clicked: method

-(void)clicked:(MyButton *)sender
{
    NSLog(@"sender = %@", sender);
    NSLog(@"sender indexPath = %@", sender.indPath);
}
Community
  • 1
  • 1
TheTiger
  • 13,264
  • 3
  • 57
  • 82
0

Just Make a Subclass of UIButton and have the parameters you want to pass in it. Example:

MyButton.h

@interface MyButton : UIButton

@property (nonatomic, retain) NSString *urArg

MyButton.m

@implementation MyButton
@synthesize urArg;

Usage

MyCustomButton *urButton = [[MyCustomButton alloc] init];
urButton.urArg = @"YOUR ARGUMENT HERE";
[urButton addtarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

//      Implementation Part of Selector
- (void)btnTapped:(id)sender {

               MyButton *btn= (MyButton*)sender; 
               argType *arg = btn.urArg;
}

In This way one can get multiple arguments.

For first argument you can btn.tag = indexPath.row. For second use custom btn.

Hope it will help!

Hemang
  • 26,840
  • 19
  • 119
  • 186
user2545330
  • 408
  • 4
  • 17
0

There's one easiest way,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
     [button setTitle:@"YourArgument" forState:UIControlStateDisable];
     [button setTag:indexPath.row];
     [button addtarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnTapped:(id)sender {
     UIButton *btn= (UIButton *)sender; 
     NSString *argument = [button titleForState:UIControlStateDisabled];
     int tagID = btn.tag;
}

Note, we're setting up argument into disable state, so only use this solution if you'll not going disable your button. If you stop interaction to the button, you can disable it using, setUserInteractionEnabled to NO.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • What is need of `argument` here ? And again I'm saying that this code would no work in case of multiple sections. – TheTiger Aug 09 '13 at 15:16