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

相棒、位置情報の受信を開始した日付を追跡したいようですね。

次のようにしてはどうでしょうか。

// 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 に答える