0

IOS アプリケーションでエラーが発生します。私はグーグルとここで検索しましたが、具体的な解決策は見つかりませんでした!

アプリで 2 回使用する mapView という viewController があります。このビューには MKMapView とコードが含まれています。

私のmapView.hには次のものがあります:

@property (strong, nonatomic) IBOutlet MKMapView *mapSpot;

そして、私の mapView.m には次のものがあります。

- (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 を再度ロードしますが、この瞬間に次のメソッドが呼び出されます。

最初の ViewController がアンロードされたかどうかを確認します。

2 番目の ViewController をロードすると、MapView の新しいインスタンスが作成されますが、デリゲート メソッドは呼び出されません。

それについて何か知っている人はいますか?

ありがとう

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

編集して解決:

4

2 に答える 2

0

上記の問題は、シミュレーターを使用してアプリをテストしているために発生する可能性があり、シミュレーターが位置マップを変更しない方法が 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];
    }
}

誰かがより良い解決策を持っている場合は、ここに投稿してください!

それでおしまい!

于 2013-02-25T20:03:57.107 に答える
0

問題は、この行でビューを追加する方法にあります

[self.view addSubview:[mapView view]];

ビューのみを追加した場合、コントローラ コードは実行されません。代わりに、次のように表示する必要がありますmapView

[self presentViewController:mapView animated:YES completion:nil];
于 2013-02-20T18:12:47.393 に答える