当应用加载时,Mapview会成功加载。
然后,在第一次更改视图并返回主视图并且viewDidAppear初始化之后,Mapview会成功加载。
但是在viewDidAppear的第一个实例之后,mapview地图不会加载,背景为黄色。我必须在mapview地图再次出现之前重新启动应用程序。
这是因为我在更改视图时没有正确发布mapview吗?我正在使用ARC。
编辑(katzenhut):OP在评论中发布了此代码: Appdelegate.m
if(self.locationManager==nil){
_locationManager=[[CLLocationManager alloc] init];
_locationManager.delegate=self;
self.locationManager = _locationManager;
}
Viewcontroller.m
- (void)viewDidAppear:(BOOL)animated{
[self setValuesFromPreferences];
appDelegate.refresh = FALSE;
theMapView.showsUserLocation = YES;
[theMapView setCenterCoordinate:appDelegate.locationManager.location.coordinate zoomLevel:zoom animated:YES];
[super viewDidAppear:animated];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[theMapView setCenterCoordinate: appDelegate.locationManager.location.coordinate zoomLevel:zoom animated: YES];
}
Mapviewcontroller.m
#import "MapViewController.h"
@implementation MapViewController
@synthesize theMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
appDelegate = (theAppDelegate *)[UIApplication sharedApplication].delegate;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidAppear:(BOOL)animated
{
theMapView.showsUserLocation = YES;
pendingRegionChange = YES;
isTracking = YES;
[theMapView setCenterCoordinate:appDelegate.locationManager.location.coordinate zoomLevel:14 animated:YES];
[super viewDidAppear:animated];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if( isTracking )
{
pendingRegionChange = YES;
[theMapView setCenterCoordinate:appDelegate.locationManager.location.coordinate zoomLevel:14 animated:YES];
}
}
-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
if( isTracking && ! pendingRegionChange )
{
isTracking = NO;
}
pendingRegionChange = NO;
}
@end
已解决viewcontroller.m中以下代码的问题:
- (void)viewDidDisappear:(BOOL)animated{
[timer invalidate];
}