从其他类iOS获取变量(位置信息)

时间:2014-07-12 21:49:18

标签: ios objective-c geolocation

在“OfferViewController.m”中我有一个locationManager函数。在locationManager函数中,我得到currentLatitude和currentLongitude。现在在“OfferDetailViewController.m”中,我想通过JS将位置发送到网站。

我的问题是,在“OfferDetailViewController.m”中我没有从“OfferViewController.m”获取位置信息

希望有人可以帮助我......这是我的代码。谢谢!

OfferViewController.m:

@interface OfferViewController ()

@end
...
#pragma mark -
#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSString *currentLatitude = [[NSString alloc]
                                 initWithFormat:@"%+.6f",
                                 newLocation.coordinate.latitude];

    //NSLog(@"locationManager - latitude: %@", currentLatitude);

    NSString *currentLongitude = [[NSString alloc]
                                  initWithFormat:@"%+.6f",
                                  newLocation.coordinate.longitude];

    //NSLog(@"locationManager - longitude: %@", currentLongitude);

    OfferDetailViewController *offerDetailViewController = [[OfferDetailViewController alloc] init];
    offerDetailViewController.longTest = currentLongitude;
    offerDetailViewController.latTest = currentLatitude;
}

@end

OfferDetailViewController.m:

@interface OfferDetailViewController ()

@end
...
@synthesize latTest;
@synthesize longTest;
...
- (void)webViewDidFinishLoad:(UIWebView *)offerUrl {
    NSLog(@"webViewDidFinishLoad");

    if ([[self.offerUrl stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) {
        // UIWebView object has fully loaded.
        NSLog(@"UIWebView object has fully loaded.");

        NSLog(@"webViewDidFinishLoad - longTest: %@", longTest);
        NSLog(@"webViewDidFinishLoad - latTest: %@", latTest);
...
    }
}

2 个答案:

答案 0 :(得分:2)

由于您正在处理两个视图控制器,因此建议使用segues。 Xcode的故事板文件允许您在视觉上使用segue,只需一点程序化帮助。

<强>步骤

以下是完成问题所述内容的几个步骤。这些将在下面的答案的其余部分讨论。

  1. 在故事板中创建一个segue
  2. 在故事板中命名segue
  3. 创建一个prepareForSegue:方法&amp;在接收视图控制器中设置变量
  4. 在故事板中制作segue

    首先,我建议通过其中一个segue链接两个视图控制器。要执行此操作,请转到故事板并查看您的起始视图控制器(在本例中,它是OfferViewController)。找到将触发转换的按钮或单元格(某些UI元素)。然后点击&#34;控制&#34;键盘上的按钮,然后单击该UI元素。应出现蓝线。将该行拖动到要移动到的视图控制器(在您的情况下为OfferDetailViewController),然后释放。然后,从显示的选项中选择您喜欢的转换类型(通常,如果视图是详细视图,您要选择&#34; push&#34;,但除非您有UINavigationController设置,否则将抛出一个错误。选择&#34;模态&#34;如果你不知道我刚刚说了什么)。您的最终结果应如下所示:

    How a modal segue looks. If your segue is not modal, the icon in the middle will be different.

    在故事板中命名segue

    接下来,您要为segue命名,以便以编程方式访问它。继续,然后单击segue的中心图标。我在Xcode中的右侧栏应该翻转到看起来像这样的视图:

    Editing the segue name

    现在,在“标识符”框中,键入您的segue名称。这可以是你想要的任何东西,但要记住它是什么,因为我们稍后会使用它。我还建议概述segue识别的一般模式,这样如果你有很多segue,你就不必继续引用故事板了,但是为了得到一些东西,没有必要压力过大。

    在接收视图控制器中设置变量

    这一点我们必须以编程方式完成。 Apple有一个方便的功能,在UIViewController中定义(因此,可以从任何UIViewController访问)。该函数的名称是&#39; prepareForSegue:&#39;。让我们利用这一点。

    # pragma mark - SEGUE PREPARATION
    
    /**
     *  This method prepares to transition from THIS contoller to another controller that will     
     *  display the data in more detail.
     */
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        /* Verify this is indeed the segue you want; replace "YourSegueName" to whatever you 
            named your segue. */
        if ([[segue identifier] isEqualToString:@"YourSegueName"]) {
            OfferDetailViewController *receiverController = (OfferDetailViewController *)[segue destinationViewController];
    
            /* here you can set whatever variables you want in 'receiverController'. These 
               will be accessible in the viewDidLoad: method. An example is given below. */
            [receiverController setWhateverVariable:valueOfWhateverVariable];
        }
    }
    

    那就是它!数据应该传输到你的新视图控制器,你应该好好去!

答案 1 :(得分:0)

您可以在NSUserDefaults中存储这些双重(或字符串)值(Lat&amp; Long),并从Detail View控制器中获取它们。

在显示详细信息视图之前,必须先调用位置管理器。制作一个可以重复使用的密钥,每当您重新保存用户的新坐标时,它都会覆盖最后一个。

离。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *latitudeKey = @"somelatitudekey";
        NSString *longKey = @"..";

  [userDefaults setObject:currentLatitude forKey:latitudeKey];
  [userDefaults setObject:currentLongitude forKey:longKey];

并检索......

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *latitudeKey = @"somelatitudekey";
        NSString *longKey = @"..";
Double latitudeOnNextView = [userDefaults objectForKey:@"latitudeKey"];
Double longitdeOnNextView = [userDefaults objectForKey:@"longKey"];

确保只将坐标(双或字符串中的任何一个)存储到用户默认值中 - 如果您尝试保存位置管理器返回的整个位置对象,它将无法工作..除非您将其序列化。

此外,当您在详细信息视图中调用坐标时,如果位置管理员在将用户保存为用户默认值时尚未完成更新用户的位置,则他们很可能为空相应地检查那些nils。 **这很可能是你现在遇到的问题 - 当你打电话给你的新视图控制器时,你传递的那些值还没有。

相关问题