为什么Xcode 4中没有显示iPhone应用程序的覆盖图?

为什么Xcode 4中没有显示iPhone应用程序的覆盖图?,iphone,xcode,overlay,geometry,Iphone,Xcode,Overlay,Geometry,我尝试按照此处提供的覆盖说明进行操作: 但是覆盖层没有显示出来。以下是我到目前为止所做工作的一步一步: 1.)将新的视图控制器对象添加到情节提要 2.)将地图视图对象添加到新的视图控制器 3.)转到File,New,New File…,UIViewController子类,将其命名为“MapViewController”,它将三个文件添加到我的项目中:MapViewController.h、MapViewController.m和MapViewController.xib 4.)然后我转到视图

我尝试按照此处提供的覆盖说明进行操作:

但是覆盖层没有显示出来。以下是我到目前为止所做工作的一步一步:

1.)将新的视图控制器对象添加到情节提要
2.)将地图视图对象添加到新的视图控制器
3.)转到File,New,New File…,UIViewController子类,将其命名为“MapViewController”,它将三个文件添加到我的项目中:MapViewController.h、MapViewController.m和MapViewController.xib
4.)然后我转到视图控制器并将其类定义为MapViewController
5.)然后“控制”+将MKMapView对象单击到MapViewController.h,该对象创建:“@property(弱,非原子)IBOutlet MKMapView*MKPolygonView;
6.)然后,我将适当的代码添加到.h和.m文件中,以反映示例,我将包括下面的一份副本

当我启动iOS模拟器时,地图会显示出来,但覆盖层不会显示。我做错了什么

MapViewController.h:

//
//  MapViewController.h
//  GeoShapes
//
//  Created by Template User on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

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

@interface MapViewController : UIViewController <MKMapViewDelegate> {

    IBOutlet MKMapView *map;

}

@property (weak, nonatomic) IBOutlet MKMapView *MKPolygonView;


@end
//
//MapViewController.h
//地形
//
//模板用户于2012年4月1日创建。
//版权所有(c)2012 uu MyCompanyName uuu。保留所有权利。
//
#进口
#进口
@接口MapViewController:UIViewController{
IBMKMAPVIEW*地图;
}
@属性(弱、非原子)IBMOutlet MKMapView*MKPolygonView;
@结束
MapViewController.m:

//
//  MapViewController.m
//  GeoShapes
//
//  Created by Template User on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MapViewController.h"

@implementation MapViewController
@synthesize MKPolygonView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
}
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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

- (void)viewDidUnload
{
    [self setMKPolygonView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    // Define an overlay that covers Colorado.
    CLLocationCoordinate2D  points[4];

    points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
    points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
    points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
    points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);

    MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
    poly.title = @"Colorado";

    [map addOverlay:poly];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonView*    aView = [[MKPolygonView alloc] initWithPolygon:       (MKPolygon*)overlay];

        aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        aView.lineWidth = 3;

        return aView;
}

    return nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
//
//MapViewController.m
//地形
//
//模板用户于2012年4月1日创建。
//版权所有(c)2012 uu MyCompanyName uuu。保留所有权利。
//
#导入“MapViewController.h”
@MapViewController的实现
@综合MKPolygonView;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
}
-(无效)视图卸载
{
[self-setMKPolygonView:nil];
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
//定义覆盖科罗拉多州的覆盖图。
CLLocationCoordination2D点[4];
点[0]=CLLocationCoordinate2DMake(41.000512,-109.050116);
点[1]=CLLocationCoordinate2DMake(41.002371,-102.052066);
点[2]=CLLocationCoordinate2DMake(36.993076,-102.041981);
点[3]=CLLocationCoordinate2DMake(36.99892,-109.045267);
MKPolygon*poly=[MKPolygon polygon with坐标:点计数:4];
poly.title=@“科罗拉多”;
[地图添加覆盖:多边形];
}
-(MKOverlayView*)地图视图:(MKMapView*)地图视图覆盖:(id)覆盖
{
if([overlay iskindof类:[MKPolygon类]])
{
MKPolygonView*aView=[[MKPolygonView alloc]initWithPolygon:(MKPolygon*)覆盖];
aView.fillColor=[[UIColor cyanColor]colorWithAlphaComponent:0.2];
aView.strokeColor=[[UIColor blueColor]colorWithAlphaComponent:0.7];
aView.lineWidth=3;
返回视图;
}
返回零;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
@结束

我认为您已将代码放入
viewDidUnload
中的
viewDidUnload
中。当视图卸载时,您正在添加一个覆盖,这可能不是您想要的。

viewDidUnload
中下面的代码移动到
viewDidLoad

[self setMKPolygonView:nil];

// Define an overlay that covers Colorado.
CLLocationCoordinate2D  points[4];

points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);

MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
poly.title = @"Colorado";

[map addOverlay:poly];

将其移动到viewDidLoad,但除了回答中提到的修复外,覆盖仍不显示:在xib中,检查地图视图的代理出口是否连接到视图控制器(Ctrl单击地图视图,然后从代理出口拖动到视图控制器图标)还有,为什么在.h中声明了两个MKMapView IBOutlets(其中一个名为MKPolygonView,非常奇怪和混乱)?成功了!非常感谢!感谢下面的回答,基本上说了相同的事情,我只是选择了最具描述性的答案。