mkmapview没有根据nib文件中的框架显示

时间:2013-09-23 04:46:13

标签: xcode mkmapview

这是我的nib文件和地图视图的屏幕,但地图总是占据整个视图。我尝试重置我的模拟器并清理我的Xcode但没有任何作用。我做错了什么? 我是否需要以编程方式为mapview提供框架?

http://prntscr.com/1sy1bw

如果无法将地图设置为采用特定帧,我还希望按钮位于mapview的顶部。

请考虑以下代码:

#import "mapViewController.h"

@interface mapViewController ()

@end

@implementation mapViewController
@synthesize mapView,source,dest,latdest,latsource,longdest,longsource;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    dest=@"delhi";
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,160,240)];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
[geocoder1 geocodeAddressString:source
             completionHandler:^(NSArray* placemarks, NSError* error)
{
    for (CLPlacemark* aPlacemark in placemarks)
    {
        coordinate.latitude = aPlacemark.location.coordinate.latitude;
        latsource=&coordinate.latitude;
        coordinate.longitude = aPlacemark.location.coordinate.longitude;
        longsource=&coordinate.longitude;
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        [annotation setCoordinate:(coordinate)];
        [annotation setTitle:source];
        annotation.subtitle = @"I'm here!!!";
        mapView.delegate = self;
        [self.mapView addAnnotation:annotation];

    }
}];
}

1 个答案:

答案 0 :(得分:1)

- (void)viewDidLoad
{
[super viewDidLoad];
//mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,160,240)];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
[geocoder1 geocodeAddressString:source
             completionHandler:^(NSArray* placemarks, NSError* error)
    {
    for (CLPlacemark* aPlacemark in placemarks)
        {
        coordinate.latitude = aPlacemark.location.coordinate.latitude;
        latsource=&coordinate.latitude;
        coordinate.longitude = aPlacemark.location.coordinate.longitude;
        longsource=&coordinate.longitude;
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        [annotation setCoordinate:(coordinate)];
        [annotation setTitle:source];
        annotation.subtitle = @"I'm here!!!";
        [self.view addSubview:mapView];
        [self.mapView addAnnotation:annotation];
        }
    }];
}

我评论了mapview分配代码行,这解决了问题。像mapview这样的内容在被分配的内存空间后立即被推送。另外,我初始化mapviewview的帧协调仍然保留,虽然代码被评论。不知道是否它应该以这种方式工作,但它确实解决了我的问题。