将纬度和经度坐标发送到应用程序委托中的 didFinishLaunchingWithOptions

发布于 2024-10-20 13:16:53 字数 1413 浏览 0 评论 0原文

我有一个函数可以在我的应用程序委托中获取纬度和经度,并且工作正常:

- (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 部分吗?

I have a this function to grab the lat and long in my app delegate, and it's working fine:

- (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);
}

However, I'm trying to send the currentLatitude and currentLongitude values to the top of the app delegate in the didFinishLaunchingWithOptions section. I'm trying to do this so I can pass those values to another viewController in which I have set up:

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

Can I send the currentLatitude and currentLongitude values to the didFinishLaunchingWithOptions section?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2024-10-27 13:16:53

为什么不在第二个 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];

Why not create a currentLatitude and currentLongitude in the second viewController?

You can use

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

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

or just

[viewController setCurrentLatitude: currentLatitude];
[viewController setCurrentLongitude: currentLongitude];
め七分饶幸 2024-10-27 13:16:53

根据我对你的问题的理解,你想将这些坐标值传递给另一个视图控制器。如果是这种情况,您可以将 currentLatitude 和 currentLongitude 作为应用程序委托的类变量。然后,当您在 applicationDidFinishLaunchingWithOptions: 中调用此函数时,您可以将值分配给这些类变量。然后您可以通过应用程序委托的实例在任何地方访问这些变量。

注意:如果您想在类外部访问变量,则必须合成变量。

希望这有帮助

From what I understood from your question, you want to pass those coordinate values to another view controller. If thats the case u can make the currentLatitude and currentLongitude as class variables of the app delegate. Then when u call this function in applicationDidFinishLaunchingWithOptions: you can assign the values to these class variables. Then u can access these variables anywhere through an instance of the app delegate.

Note: You have to synthesize the variables if u want to access them outside the class.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文