2011-03-08 60 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