0

将 var 传递给另一个函数的最简单方法是什么?

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation {

   NSLog(@"%@", started);
}

我试过了:

定义了一个全局变量:

extern NSString *started;

当我直接设置 NSString 并传递给另一个函数时,它运行良好:

-(void) startTracking:(CDVInvokedUrlCommand*)command {
  started = @"testing";
}

但它不起作用:

-(void) startTracking:(CDVInvokedUrlCommand*)command {

  NSString* myarg = [command.arguments objectAtIndex:0]; // http://docs.phonegap.com/en/2.5.0/guide_plugin-development_ios_index.md.html#Developing%20a%20Plugin%20on%20iOS_writing_an_ios_cordova_plugin
  started = myarg;
}

(本人是objective-C初学者,不是很懂)

编辑:似乎只有当我将应用程序置于后台时它才会崩溃。

4

2 回答 2

0

根据您是否使用 ARC,您必须保留该对象。你可能想在你的类上使用一个属性:

在您的标题中:

@property(strong) NSString *started;

在实施中:

-(void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
       fromLocation:(CLLocation *)oldLocation {

 NSLog(@"%@", self.started);
}

-(void) startTracking:(CDVInvokedUrlCommand*)command {
  self.started = @"testing";
}

-(void) startTracking:(CDVInvokedUrlCommand*)command {

 NSString* myarg = [command.arguments objectAtIndex:0];
 self.started = myarg;
}
于 2013-05-24T11:55:15.363 回答
0

伙计,您似乎想跟踪您开始接收位置信息的日期。

这样做怎么样:

// Your .h file
@interface MyClass <CLLocationManagerDelegate>
{
    BOOL hasStartedUpdatingLocation;
    NSDate *startDate;

    CLLocationManager *locationManager;
}

...

// Your .m file
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation 
{
    // ---------------------------------------------------------------
    // if has NOT started updating location, record start date
    // otherwise, do not execute this if statement
    // ---------------------------------------------------------------
    if(!hasStartedUpdatingLocation)
    {
        hasStartedUpdatingLocation = YES;

        // this if statement should only execute once
        startDate = [NSDate date]; // get the current date and time
    }
    else
    {
        // do something else
    }
}
于 2013-05-24T12:31:45.377 回答