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

我在这里设置了结果标签:

@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.

大概你的问题是resultsLabelnil你尝试设置它的文本时。发送消息到nil什么都不做,甚至不会给出错误。

于 2012-11-19T13:12:44.463 回答