IBActions & XIB Files

Discussion in 'iOS Programming' started by skm, Sep 8, 2009.

  1. skm macrumors newbie

    Joined:
    Jun 21, 2009
    #1
    Hey,

    I'm just starting to learn iPhone programming and come across one thing that seemed a little strange that I thought I would ask about.

    http://blog.objectgraph.com/index.php/2009/04/03/iphone-sdk-30-playing-with-map-kit-part-2/

    I was going through this website and completed the same program. When I was finished I seen some code that I thought might work a bit better.

    Code:
    @interface MainViewController : UIViewController <FlipsideViewControllerDelegate,MKReverseGeocoderDelegate,MKMapViewDelegate> {
    	MKMapView *mapView;
    	MKReverseGeocoder *geoCoder;
    	MKPlacemark *mPlacemark;
    	IBOutlet UISegmentedControl *mapType;
    }
    - (IBAction)changeType:(id) sender;
    
    --------------------------------------------
    
    - (IBAction)changeType:(id)sender{
    	if(mapType.selectedSegmentIndex==0){
    		mapView.mapType=MKMapTypeStandard;
    	}
    	else if (mapType.selectedSegmentIndex==1){
    		mapView.mapType=MKMapTypeSatellite;
    	}
    	else if (mapType.selectedSegmentIndex==2){
    		mapView.mapType=MKMapTypeHybrid;
    	}
    }
    
    I seen this and then changed it to this:

    Code:
    @interface MainViewController : UIViewController <FlipsideViewControllerDelegate,MKReverseGeocoderDelegate,MKMapViewDelegate> {
    	MKMapView *mapView;
    	MKReverseGeocoder *geoCoder;
    	MKPlacemark *mPlacemark;
    }
    - (IBAction)changeType:(UISegmentedControl*) sender;
    
    --------------------------------------------
    
    - (IBAction)changeType:(UISegmentedControl*)sender{
    	if(sender.selectedSegmentIndex==0){
    		mapView.mapType=MKMapTypeStandard;
    	}
    	else if (sender.selectedSegmentIndex==1){
    		mapView.mapType=MKMapTypeSatellite;
    	}
    	else if (sender.selectedSegmentIndex==2){
    		mapView.mapType=MKMapTypeHybrid;
    	}
    }
    
    I removed the need to have the IBOutlet in the code and (I thought) made it a bit simpler. This restricts the changeType method to only be linked with UISegmentedControl objects in the XIB file but that's all it was being used for anyway.

    Looking around I often see alot of delegate methods using (id)sender. Is there any particulate reason for this?

    The only reason I can think you would use (id)sender is if objects of different class types would be calling that delegate method, other then that using the particular class is Ok.

    I'm mainly asking because I see it around alot, the use of (id)sender - and wonder if I should be doing the same thing for any reason.

    Thanks
     
  2. skm thread starter macrumors newbie

    Joined:
    Jun 21, 2009
    #2
    Also looking at this site:

    http://icodeblog.com/2009/07/27/1188/

    It also uses (id)sender.

    I see that this could use (UIWebView)sender but would also need to keep the IBOutlet because it uses it in viewDidLoad.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Code:
    - (IBAction)myAction:(id) sender;
    is the canonical prototype. I don't see anything wrong with changing the type of sender to the object that will be calling the action method if you like. It's a bit more fragile in that if you make a change or connect something up incorrectly you'll probably get runtime errors but the usual way to support the actions is to typecast sender to the expected type. Your change is an implicit typecast.

    When sending the action message the type of the parameter isn't checked at runtime so the action should always be received correctly.
     

Share This Page