将纬度和经度坐标发送到app委托中的didFinishLaunchingWithOptions

时间:2011-03-08 04:40:13

标签: iphone objective-c geolocation latitude-longitude

我有一个这个函数来抓取我的app委托中的lat和long,并且它工作正常:

- (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部分中的app委托的顶部。我正在尝试这样做,所以我可以将这些值传递给我已经设置的另一个viewController:

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

我可以将currentLatitude和currentLongitude值发送到didFinishLaunchingWithOptions部分吗?

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

根据我对您的问题的理解,您希望将这些坐标值传递给另一个视图控制器。如果是这种情况,你可以将currentLatitudecurrentLongitude作为app委托的类变量。然后,当您在applicationDidFinishLaunchingWithOptions:中调用此函数时,您可以将值分配给这些类变量。然后你可以通过app delegate的实例随处访问这些变量。

注意:如果你想在课堂外访问它们,你必须综合变量。

希望这有帮助