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

.m ファイルで theLabel を合成していますか? そうでない場合は、そうする必要があると思います。

于 2011-04-04T18:56:56.523 に答える