0

我想用三个段将 SegmentedControl 添加到我的 MKMapView

  1. 标准视图
  2. 卫星视图
  3. 混合视图

我们可以通过点击线段来改变地图类型

我尝试了一些代码

- (void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
segControl= [[UISegmentedControl alloc] initWithItems:itemArray];

if((segControl.selectedSegmentIndex = 0)){
    _mapView = MKMapTypeStandard;
}
if((segControl.selectedSegmentIndex = 1)){
    _mapView = MKMapTypeSatellite;
}
else {
    _mapView = MKMapTypeHybrid;
}

}

4

2 回答 2

2

嘿试试这个条件segControl.selectedSegmentIndex == 0而不是这个segControl.selectedSegmentIndex = 0

这里有单 = 符号的问题

并且也尝试像下面的代码..

- (void)viewDidLoad
{
      segControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects: @"One", @"Two", @"Three", nil]];
      [segControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
      [segControl setFrame:CGRectMake(50,20,200,44)];//set frame which you want
      [self.view addSubview:segControl];
}

- (void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
    switch (aSegmentedControl.selectedSegmentIndex) {
            case 0:
                map.mapType = MKMapTypeStandard;
                break;
            case 1:
                map.mapType = MKMapTypeSatellite;
                break;
            case 2:
                map.mapType = MKMapTypeHybrid;
                break;

            default:
                break;
        }
}
于 2012-12-13T11:51:10.477 回答
0

听听UIControlEventValueChanged。因此,将您的 segmentedControl 连接到IBAction这样的:

-(IBAction)changeMapType:(id)sender{
   UISegmentedControl *seg=(UISegmentedControl*)sender;
   if(seg.selectedSegmentIndex==0){//not = but ==
      _mapView.mapType=MKMapTypeStandard;
   }
   if(seg.selectedSegmentIndex==1){
      _mapView.mapType=MKMapTypeSatellite;
   }
   if(seg.selectedSegmentIndex==2){
      _mapView.mapType=MKMapTypeHybrid;
   }
}
于 2012-12-13T11:55:51.340 回答