tags:

views:

51

answers:

2

I want to get the name of a sender in Objective-C. For example, below I have a method which is called by an instance of UISlider in Interface Builder, I want to know what the instance name of it is so I can later add conditional blocks to the method for which instance of UISlider called the method.

e.g.

-(IBAction)sliderChanged:(UISlider *)sender {
    //labAt1TimeRequired.text = [NSString stringWithFormat:@"%.1f", [sender value]]; 

    NSLog(@"%@",sender);

Outputs:2010-10-15 22:46:02.257 EPC[3225:207] <UISlider: 0x495b140; frame = (205 3; 118 23); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x492e340>>

I want to be able to say

if(sender==myInstanceName)  { 
//do this 
}
A: 

You could use

.tag member

to read an write and integer ID for the slider like this:

-(IBAction)sliderChanged:(UISlider
 *)sender { 
   switch (sender.tag) {
    case 0:
       //SLider 0
       break;
      case 1:
       //SLider 1
       break;
      default:
       break;
     }
    }

Tag ID's can also be set for components in IB.

If your set on a string then you would need to subclass a UISlider.

Luke Mcneice
A: 

You would use the tag property of UIView for identifying the sender.

-(IBAction)sliderChanged:(UISlider *)sender {
//labAt1TimeRequired.text = [NSString stringWithFormat:@"%.1f", [sender value]]; 

    if (sender.tag == 1)
    {
        // do whatever
    }
    else
    {
        // do something else
    }
}
willcodejavaforfood