9

我被卡住了:(
在我的应用程序中,每次更新到新位置时,我都需要从 CLLocationManager 进行更新。我没有使用 XIB/NIB 文件,我编写的所有代码都是以编程方式完成的。代码:
.h


@interface TestViewController : UIViewController
    UILabel* theLabel;

@property (nonatomic, copy) UILabel* theLabel;

@end

他们


...

-(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]];

}
...

有什么想法或帮助吗?

(这全是手卡,所以如果看起来有点笨拙,请原谅我)如果需要,我可以提供更多信息。

4

2 回答 2

16

您的 loadView 方法是错误的。您没有正确设置实例变量,而是生成了一个新的局部变量。通过省略 将其更改为以下内容UILabel *并且不要释放它,因为您希望保留对标签的引用以稍后设置文本。

-(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]];

 }
于 2011-04-04T18:40:51.223 回答
0

Are you synthesizing theLabel in your .m file...? If not, you need to, I believe.

于 2011-04-04T18:56:56.523 回答