user141302
user141302

Reputation:

How can I send two arguments in a selector method?

In the following, how can I send two arguments in togButton method? I tried, but it did not work.

[button addTarget:self action:@selector(togButton:) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 2

Views: 3601

Answers (6)

Sumit M Asok
Sumit M Asok

Reputation: 2950

[Class instancesRespondToSelector: @selector (setTo:over:)]

I remember seeing this Objective-C code in a book.

Upvotes: -2

gerry3
gerry3

Reputation: 21460

The action selector called by a button tap can have zero, one, or two parameters. The first is the sender (the button that was tapped) and the second is the event (the tap). See the target-action mechanism for controls:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

As Thomas Müller suggests, the best way to pass app-specific data to your action selector is by subclassing UIButton (or, in the case of a button on a UITableViewCell, you can add the app-specific data to a UITableViewCell subclass and access the cell as the button's superview.superview).

Upvotes: 1

Khaled Annajar
Khaled Annajar

Reputation: 16592

This is an example of a work around to get data from UI component

-(void) switchChanged:(id) sender
{
    UISwitch* switchControl = (UISwitch*)sender;
    UITableViewCell *cell = (UITableViewCell*)[sender superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    cell.textLabel.text =" change text title";
}

now you have cell and indexPath and you can do what you want. I hope I helped.

Also you can use structs to wrap your arguments in one parameter.

Upvotes: 1

zpesk
zpesk

Reputation: 4353

The best way to send two arguments to the method is to wrap them in a class. For example, if the two are arguments are strings then you would have a class solely comprised of string iVars.

Upvotes: 0

Thomas Müller
Thomas Müller

Reputation: 15635

What are you trying to put in there? Maybe you can use the tag property, or if that's not sufficient, subclass UIButton and add instance variables that you can then access in -togButton:.

Upvotes: 2

Ben Gottlieb
Ben Gottlieb

Reputation: 85542

control targets only accept one argument for their actions: the control that's being manipulated. There's no way to do what you want (the OS just doesn't support it).

Upvotes: 4

Related Questions