lab12
lab12

Reputation: 6448

Pass 2 Objects through a Method - iPhone SDK

I have this "observer" that watches the UISwitch for a change in value:

[cell.switcher addTarget:self action:@selector(switched:withName:) forControlEvents:UIControlEventValueChanged];

When the value is changed this method is called:

-(void)switched:(UISwitch *)switcher withName:(NSString *)name;

As you can see I have two objects which I need to pass with the first code. How do I send the object of a nsstring and a uiswitch through the method of addTarget:action:forControlEvents so that I can access them in the selector switched:withname?

Thanks,

Kevin

EDIT: I'm not very good with obj-c and didn't really learn the terms correctly, so if I made a mistake please let me know!

Upvotes: 0

Views: 319

Answers (2)

Rog
Rog

Reputation: 18670

Best way to go about this is to tag your UISwitch objects:

uiSwitch1.tag = 0;
uiSwitch2.tag = 1;
...

Then on your switched: method you can test the sender's tag and define your string there:

-(void)switch:(id)sender {
    switch ([sender tag]) {
        case 0:
        // set the string for uiSwitch1
        case 1:
        // set the string for uiSwitch2
        ...
    }
}

Upvotes: 3

David Liu
David Liu

Reputation: 9600

You can't. The selector for UIControl actions will only pass back the sender (in this case, your cell.switcher). You should find a way to identify which string you need based on the action and the id of the sender.

Upvotes: 1

Related Questions