-1

我的应用程序中有几个地图视图。尝试添加一个简单的分段控制器开关,以使用户能够选择地图、卫星、混合地图。所有指令似乎都与谷歌地图有关;我正在使用 Apple mapkit。

更新

.h 文件

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface TrucksViewController : UIViewController {

}

- (IBAction)setMap:(id)sender;

@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *mapType;

@end

.m 文件

#import "TrucksViewController.h"

@interface TrucksViewController ()


@end

@implementation TrucksViewController
@synthesize myMapView;

#pragma mark -
#pragma mark Initialisation

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
    // Set the title for this view controller
    // Note: In future we will copy over the title from any created     UINavigationBar objects
    self.title = @"Trucks";

    [[UIApplication sharedApplication] setStatusBarHidden:NO     withAnimation:UIStatusBarAnimationNone];
}
return self;
}

#pragma mark -
#pragma mark UIViewController Delegates

- (IBAction)setMap:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        myMapView.mapType = MKMapTypeStandard ;
        break;
    case 1:
        myMapView.mapType = MKMapTypeSatellite ;
        break;
    case 2:
        myMapView.mapType = MKMapTypeHybrid ;
        break;

    default:
        break;
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
[super viewDidUnload];
}

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// Update support iOS 7
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
}
}

-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

// Revert to default settings
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
    self.edgesForExtendedLayout = UIRectEdgeAll;
}
}

- (void)viewDidLoad 
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}


@end
4

1 回答 1

0

给你男人..

在您的 .h 文件中声明一个 ibaction,如下所示:

- (IBAction)setMap:(id)sender;

然后在您的 .m 文件中为其创建包含分段控制器的方法。如下:

- (IBAction)setMap:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        myMapView.mapType = MKMapTypeStandard ;
        break;
    case 1:
        myMapView.mapType = MKMapTypeSatellite ;
        break;
    case 2:
        myMapView.mapType = MKMapTypeHybrid ;
        break;

    default:
        break;
}
}

不要忘记在您的 ib 中连接分段控件。上面也有三个段,所以你必须自己添加第三个,因为对象库中的分段控件默认有两个。

于 2013-12-15T05:25:12.480 回答