在updatetonewlocation时保持NULL

时间:2012-11-19 12:58:41

标签: ios null cllocationmanager

- (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)

on:

       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;

1 个答案:

答案 0 :(得分: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发送消息不会做任何事情,甚至不会出错。