Search code examples
ioscurldictionaryuiviewcontrollertransfer

changing MKMap type like in "map" app on iphone


I am currently trying to change the maptype of my map (from MapsViewController) with the help of a segmentedcontrol from another viewcontoller (BackgroundMapViewController). I use page curl to see the viewcontoller with the segmentedcontol (like the map app on iphone) the problem is that I don´t know how to pass the information from my BackgroundMapViewController to my MapsviewController. (probably with delegation ??) I understood that you use delegation to give some "work" of an object to an other object, but I don´t know how I should use it here. I would really appreciated if someone could help me out with my problem

(you can see a picture of the UI here: http://i50.tinypic.com/169masx.png )

MapsBackgrounsViewController.h

    @interface MapBackgroundViewController : UIViewController{
    IBOutlet UISegmentedControl *segmentedControl;
    MKMapType mapType;
    }

    @property (nonatomic) IBOutlet UISegmentedControl *segmentedControl;
    @property(nonatomic) MKMapType mapType;
    - (IBAction)segmentedControllChanged:(id)sender;

MapsViewController.m

    @interface MapBackgroundViewController ()
    @end

    @implementation MapBackgroundViewController
    @synthesize segmentedControl, mapType;

    - (IBAction)segmentedControllChanged:(id)sender {

    if (segmentedControl.selectedSegmentIndex == 0) {
        mapType = MKMapTypeStandard;
    }else if (segmentedControl.selectedSegmentIndex == 1) {
        mapType = MKMapTypeSatellite;
    } else if (segmentedControl.selectedSegmentIndex == 2) {
       mapType = MKMapTypeHybrid;
    }


    [self dismissModalViewControllerAnimated:YES];

}

MapsViewController.h

   @interface MapsViewController : UIViewController<MKMapViewDelegate,   UISearchBarDelegate>{
   @private
   IBOutlet MKMapView *map;
   //some other outlet
   }

   @property (nonatomic, retain) IBOutlet MKMapView *map;
   @property (nonatomic, retain) IBOutlet  UISearchBar *searchBar;
   //some other actions and properties

Solution

  • You don't need to store mapType, just set it directly in the map view:

    @interface MapBackgroundViewController ()
    @end
    
    @implementation MapBackgroundViewController
    @synthesize segmentedControl, mapViewController; // Connect mapViewController to your other view controller
    
    - (IBAction)segmentedControllChanged:(id)sender {
    
    if (segmentedControl.selectedSegmentIndex == 0) {
        self.mapViewController.map.mapType = MKMapTypeStandard;
    }else if (segmentedControl.selectedSegmentIndex == 1) {
        self.mapViewController.map.mapType = MKMapTypeSatellite;
    } else if (segmentedControl.selectedSegmentIndex == 2) {
       self.mapViewController.map.mapType = MKMapTypeHybrid;
    }
    
    
    [self dismissModalViewControllerAnimated:YES];