2011-12-29 25 views
0

上位レベルのビューコントローラで[マップ]ボタンをクリックしたときに、新しいビューコントローラにピン注釈を表示したいとします。2つのViewController間でマップキットの変数を共有するにはどうすればよいですか?

私は下のように上位レベルのコントローラのメソッドファイルで "IBAction"メソッドを使用しました。 次に、緯度と経度の値が(NSLogの)プロパティリストから正常に表示されました。 しかし、私は新しいビューコントローラでピン注釈を見ることができません。

しかし、「viewDidLoadのコード」というコードを新しいビューコントローラ(「location」という名前)に配置すると、ピンの注釈が表示されます。 しかし、緯度と経度の値は0.00000です。

変数が2つのView Controller間で共有されていないと思います。 この問題を解決するのを手伝ってください。

- (IBAction) goAddView:(id)sender { 

// the code for viewDidLoad 
    double myLat = [[drink objectForKey:lati_KEY] doubleValue]; 
    double myLong = [[drink objectForKey:long_KEY] doubleValue];    CLLocationCoordinate2D theCoordinate; 
    theCoordinate.latitude = myLat; 
    theCoordinate.longitude = myLong; 
    NSLog(@"the latitude = %f",theCoordinate.latitude); 
    NSLog(@"the longitude = %f",theCoordinate.longitude); 

    myAnnotation *myAnnotation1=[[myAnnotation alloc] init]; 

    myAnnotation1.coordinate=theCoordinate; 
    [email protected]"Destination"; 
    [email protected]"in the city"; 
    [self.mapView addAnnotation:myAnnotation1]; 
// the code end 

    location *lm= [[location alloc] initWithNibName:@"location" bundle:nil]; 
    [self.navigationController pushViewController:lm animated:YES]; 

答えて

1

あなたが共有したい変数がdrinkであると仮定します。両方のビューコントローラでdrinkをivarとして宣言しただけでは、自動的に "共有"されません。 + goAddViewに+ init locationを割り当てると、drinknilになり、lmになります。 locationviewDidLoadメソッドは、プッシュ/プッシュすると呼び出されます。

値をlocationに渡す1つの方法は、プロパティを使用しています。まず、locationビューコントローラにプロパティとしてdrinkを宣言します

//in location.h: 
@property (retain) NSDictionary *drink; 
//in location.m: 
@synthesize drink; 
//and release in dealloc if not using ARC 

次に、あなたのalloc後にプロパティを設定+ goAddViewでそれを初期化し、前にpushViewControllerを呼び出す:

- (IBAction) goAddView:(id)sender 
{ 
    location *lm = [[location alloc] initWithNibName:@"location" bundle:nil]; 

    lm.drink = drink; //point drink in lm to the one in current vc 

    [self.navigationController pushViewController:lm animated:YES]; 

    //and do [lm release]; if not using ARC 
} 
+0

感謝をあなたの答えは大変です。私はもっ​​と勉強する必要があります。あなたに幸せな新年と幸運:-)どうもありがとう。 –