Search code examples
iosiphoneobjective-cuibuttongoogle-maps-sdk-ios

iOS- Howto change the image and property of a button by clicking it?


In my app, I am having a mapView and a button on it, the button is used to toggle the mapView between a standard mapView and the satellite view.

enter image description here
Here is the snap shot, if I press the satellite button, the mapView should get changed to sattelite view and the button image should get changed to the following image,
enter image description here
And now wen the button is pressed it should get changed to a normal mapView.

//mapView
camera=[GMSCameraPosition cameraWithLatitude:13.45 longitude:80.93 zoom:6];
mapView=[GMSMapView mapWithFrame:CGRectMake(0, 0, width, height) camera:camera];
mapView.myLocationEnabled=YES;  
//button
satellite=[UIButton buttonWithType:UIButtonTypeCustom];
satellite.Frame=CGRectMake(250, 300, 65 , 65);
[satellite setBackgroundImage:[UIImage imageNamed:@"Satellite.png"] forState:UIControlStateNormal];
[satellite addTarget:self action:@selector(satelliteView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapView];
[self.view addSubview:satellite];

Solution

  • Simple :

    Try using the tag for the UIButton . By default assign tag as for standard mapView For Ex mapButton.tag = 100;

        - (IBAction)mapSatelliteSegmentControlTapped:(UIButton *)sender
        {
            if(sender.tag == 100)
            {
                  self.mapView.mapType = MKMapTypeStandard;
                  // Change the MapButton image as well as tag 
                   self.mapButton.tag = 101; // 
                  [self.mapButton setImage:[UIImage imageNamed:@"mapSateliite.png"] forState:UIControlStateNormal];
            }
            else
            {
                  self.mapView.mapType = MKMapTypeSatellite;
                  // Change the MapButton image as well as tag 
                  self.mapButton.tag = 100; // 
                  [self.mapButton setImage:[UIImage imageNamed:@"mapStandard.png"] 
            }
    
    }