我被困住了:(
在我的应用程序中,每次更新到新位置时,我都需要 CLLocationManager 进行更新。我没有使用 XIB/NIB 文件,我编码的所有内容都是以编程方式完成的。到代码:
the .h
@interface TestViewController : UIViewController
UILabel* theLabel;
@property (nonatomic, copy) UILabel* theLabel;
@end
the .m
...
-(void)loadView{
....
UILabel* theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
theLabel.text = @"this is some text";
[self.view addSubView:theLabel];
[theLabel release]; // even if this gets moved to the dealloc method, it changes nothing...
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location: %@", [newLocation description]);
// THIS DOES NOTHING TO CHANGE TEXT FOR ME... HELP??
[self.view.theLabel setText:[NSString stringWithFormat: @"Your Location is: %@", [newLocation description]]];
// THIS DOES NOTHING EITHER ?!?!?!?
self.view.theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]];
}
...
有什么想法或帮助吗?
(这都是手工卡住的,所以如果看起来有点混乱,请原谅我)如果需要,我可以提供更多信息。
你的loadView方法是错误的。您没有正确设置实例变量,而是生成了一个新的局部变量。将其更改为以下内容并省略UILabel *
and 不要释放它因为您想保留对标签的引用以便稍后设置文本。
-(void)loadView{
....
theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
theLabel.text = @"this is some text";
[self.view addSubView:theLabel];
}
- (void) dealloc {
[theLabel release];
[super dealloc];
}
然后直接访问变量,如下所示:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location: %@", [newLocation description]);
theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)