0
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

NSString *latitude =  [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
NSString *longitude =  [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
NSString *stringFromDate = [formatter stringFromDate:newLocation.timestamp];

resultsLabel.text = [NSString stringWithFormat:@"(%@) %@ Location %.06f %.06f %@", ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ? @"bg" : @"fg", resultsLabel.tag == 0 ? @"gps:" : @"sig" , newLocation.coordinate.latitude, newLocation.coordinate.longitude, [formatter stringFromDate:newLocation.timestamp]];

NSLog(@"%@", resultsLabel.text);

LocationTestAppDelegate * appDelegate = (LocationTestAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate log:resultsLabel.text];




}

しかし、私はこれを出力として取得します。

(null)

の上 :

       NSLog(@"%@", resultsLabel.text);

誰かが私が間違っていることを知っていますか?

  - (id) initWithLabel:(UILabel*)label
{
resultsLabel = label;
return [super init];
}

そして、私の代理人では、ここにresultLabelを設定しました:

- (void) log:(NSString*)msg
{
 NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSString * logMessage = [NSString stringWithFormat:@"%@ %@", [formatter stringFromDate:[NSDate date]], msg];
NSString * fileName = [self locationPath];
FILE * f = fopen([fileName UTF8String], "at");
fprintf(f, "%s\n", [logMessage UTF8String]);
fclose (f);}

編集

GPS機能を削除しました。

新しい場所に適切なNSLogがどのように配置されるかを誰かが知っていますか。経度緯度タイムスタンプ?

編集2

ここでresultlabelを設定します:

@interface  LocationDelegate : NSObject <CLLocationManagerDelegate> 
{
UILabel * resultsLabel;
NSString * _phonenumber;


}

- (id) initWithLabel:(UILabel*)label;
//-(id)initWithDictionary:(NSDictionary *)dict;
//-(id)initWithName:(NSString *)aName;
//-(NSDictionary *)toDictionary;

@property (nonatomic , retain) NSString *phonenumber;
4

1 に答える 1

1

あなたのinitコードは奇妙です。これはより一般的です:

- (id) initWithLabel:(UILabel*)label
{
    if (self = [super init]) {
        resultsLabel = label; // Or maybe _resultsLabel, depending on your code
    }
    return self;
}

そして、を介してインスタンス変数であると私が推測するものにアクセスしますself.resultsLabel

おそらくあなたの問題は、あなたがそれのテキストを設定しようとしたときにresultsLabelすでにあるということです。nilにメッセージを送信しnilても何も起こらず、エラーも発生しません。

于 2012-11-19T13:12:44.463 に答える