Ios didUpdateUserLocation未在重新加载视图的情况下调用

Ios didUpdateUserLocation未在重新加载视图的情况下调用,ios,delegates,mapkit,mkmapviewdelegate,Ios,Delegates,Mapkit,Mkmapviewdelegate,我的IOS应用程序出错。我在谷歌和这里搜索过,但没有找到具体的解决方案 我有一个名为mapView的viewController,我在我的应用程序中用了两分钟,这个视图包含一个MKMapView和代码 在我的mapView.h中有: @property (strong, nonatomic) IBOutlet MKMapView *mapSpot; - (void)viewDidLoad { [super viewDidLoad]; [mapSpot setShowsUser

我的IOS应用程序出错。我在谷歌和这里搜索过,但没有找到具体的解决方案

我有一个名为mapView的viewController,我在我的应用程序中用了两分钟,这个视图包含一个MKMapView和代码

在我的mapView.h中有:

@property (strong, nonatomic) IBOutlet MKMapView *mapSpot;
- (void)viewDidLoad {
    [super viewDidLoad];

    [mapSpot setShowsUserLocation:YES];
}

- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{    
    MKCoordinateRegion region           = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 500, 500);
    [mapSpot setRegion:region animated:YES];
}
在我的mapView.m中有:

@property (strong, nonatomic) IBOutlet MKMapView *mapSpot;
- (void)viewDidLoad {
    [super viewDidLoad];

    [mapSpot setShowsUserLocation:YES];
}

- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{    
    MKCoordinateRegion region           = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 500, 500);
    [mapSpot setRegion:region animated:YES];
}
因此,我首先使用以下命令将mapView加载到其他ViewController中:

@property (strong, nonatomic) ViewMap *mapView;

mapView                         = [[ViewMap alloc] initWithNibName:@"ViewMap" bundle:nil];
[self.view addSubview:[mapView view]];
我卸载了该ViewController,并在另一个ViewController中再次加载了MapView,但在这一时刻,没有调用方法:-(void)MapView:(MKMapView*)MapView didUpdateUserLocation:(MKUserLocation*)userLocation

我验证第一个ViewController是否已卸载,并且是否已卸载

当我加载第二个ViewController时,有一个新的MapView安装,但没有调用委托方法

有人知道吗

谢谢

==================================================================================


编辑并解决:

问题在于您在此行中添加视图的方式

[self.view addSubview:[mapView view]];
如果仅添加视图,则不会执行控制器代码,而必须显示
地图视图,例如:

[self presentViewController:mapView animated:YES completion:nil];

上述问题可能是因为我正在使用模拟器测试应用程序,以及模拟器如何不更改位置映射而不获取didUpdateUserLocation:

这是我在检查代码、组织类、阅读文档并再次得到错误后可以得到的唯一解释

现在,我正在使用CLLocationManager获取位置,在获得第一次位置后,我停止它

在未来,我将实现一个跟踪用户路径的系统,因此使用CLLocationManager是不可避免的

更改后的mapView.m代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager                     = [[CLLocationManager alloc] init];
    locationManager.delegate            = self;
    locationManager.distanceFilter      = kCLDistanceFilterNone;
    locationManager.desiredAccuracy     = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *loc = [locations lastObject];

    //  store the location to use in any moment, it needs to be checked because the first time when get the coordinate not pass infos to load places according the current position
    if (!location.latitude) {
        location                            = [loc coordinate];

//      set center the map at the current position
        MKCoordinateRegion region           = MKCoordinateRegionMakeWithDistance(location, 500, 500);
        [mapSpotView setRegion:region animated:YES];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"loadPlaces" object:nil];

        [locationManager stopUpdatingLocation];
    }
}
如果有人有更好的解决方案,请在这里发布


就这样

这不是问题所在,我尝试使用presentViewController,但问题没有解决,在iphone presentViewController中全屏加载视图,在我的应用程序中不可能。另一个问题是,为什么在我第一次加载和重新加载相同视图时工作正常,而在我更改de视图时不工作?谢谢你应该将你的答案作为答案发布,并接受你自己的答案。答案与答案一起添加,谢谢答案在哪里?我也有同样的问题