2011-03-08 49 views
0

我有這個功能搶lat和長在我的應用程序委託,和它的正常工作:發送緯度和經度座標的didFinishLaunchingWithOptions在應用程序委託

- (void)newPhysicalLocation:(CLLocation *)location { 

    // Store for later use 
    self.lastKnownLocation = location; 

    // Remove spinner from view 
    for (UIView *v in [self.viewController.view subviews]) 
    { 
     if ([v class] == [UIActivityIndicatorView class]) 
     { 
      [v removeFromSuperview]; 
      break; 
     } 
    } 

    // Alert user 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Found" message:[NSString stringWithFormat:@"Found physical location. %f %f", self.lastKnownLocation.coordinate.latitude, self.lastKnownLocation.coordinate.longitude] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 

    currentLatitude = (self.lastKnownLocation.coordinate.latitude); 
    NSLog(@"current latitude: %f",self.lastKnownLocation.coordinate.latitude); 


    currentLongitude = (self.lastKnownLocation.coordinate.longitude); 
    NSLog(@"current longitude: %f",currentLongitude); 
} 

不過,我想將currentLatitude和currentLongitude值發送到didFinishLaunchingWithOptions部分中的應用程序委託的頂部。我試圖做到這一點,所以我可以將值傳遞到另一個的viewController中,我已經設置了:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 

我可以發送currentLatitude和currentLongitude值的didFinishLaunchingWithOptions部分?

回答

0

從我的理解你的問題,你想傳遞這些座標值到另一個視圖控制器。如果是這樣的話,你可以使currentLatitudecurrentLongitude作爲應用程序委託的類變量。然後當你在applicationDidFinishLaunchingWithOptions:中調用這個函數時,你可以給這些類變量賦值。然後你可以通過應用程序委託的實例在任何地方訪問這些變量。

注意:如果你想在課堂外訪問它們,你必須綜合變量。

希望這有助於

1

爲什麼不創建第二的viewController一個currentLatitudecurrentLongitude

您可以使用

- (void)newPhysicalLocation:(CLLocation *)location { 
    // your code here 

    SecondViewController *viewController = [[SecondViewController alloc] init]; 
    [viewController setLatitude: currentLatitude andLongitude: currentLongitude]; 
} 

或只是

[viewController setCurrentLatitude: currentLatitude]; 
[viewController setCurrentLongitude: currentLongitude]; 
+0

感謝,我也類似於此的實施,並得到它的工作 – Aaron 2011-03-09 13:34:45

+0

請接受和/或投票,如果答案是有幫助您。其他人可能會找到與他們的問題相關的答案。謝謝,不客氣。 – Angelo 2011-03-10 03:00:20

相關問題