- (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];
}
在我的委托中,我在这里设置了结果标签:
- (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, "%sn", [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;
你的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
发送消息不会执行任何操作,甚至不会给出错误。