Search code examples
iosobjective-cselfibactionsender

self IBAction and sender


I am a newbie iOS programmer, here is my question: I have mapview and segmented control, also changeMapType function which get called when UIControlEventValueChanged occures, like this

// change map type with segmented control
- (IBAction)changeMapType:(id)sender
{
    NSInteger i = [mapTypeControl selectedSegmentIndex];
    if (i == 0){
        [worldView setMapType:MKMapTypeStandard];
    }
    if (i == 1) {
        [worldView setMapType:MKMapTypeSatellite];
    }
    if (i == 2) {
        [worldView setMapType:MKMapTypeHybrid];
    }
}

and in viewDidLoad I want to call this method to set up which map type is first.

[mapTypeControl setSelectedSegmentIndex:2];
[self changeMapType:nil];

above code works fine, but below code works fine either

[mapTypeControl setSelectedSegmentIndex:2];
[self changeMapType:self];

so finally, what to pass as SENDER? which is correct?


Solution

  • Since you're not really doing anything with "sender" (being passed into your IBAction method), you can pass pretty much anything you want along.

    Use "self" or "nil" and you should be just fine.

    If you do end up using "sender" within any of your IBActions, then you do need to care about what you send along programatically. When you click a button or some object in a user interface that's connected to your action, a reference to that object is what gets sent.

    p.s. it's like an answer party in here!