Как обновить UILabel в Xcode программно без XIB-файлов?
Я застрял :(
В моем приложении мне требуется обновление от CLLocationManager каждый раз, когда он получает обновление до новой позиции. Я не использую файлы XIB/NIB, все, что я закодировал, я сделал программно. Код:
этот.h
@interface TestViewController : UIViewController
    UILabel* theLabel;
@property (nonatomic, copy) UILabel* theLabel;
@end
the .м
...
-(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]];
}
...
любые идеи или помощь?
(это все рука застряла, поэтому, пожалуйста, простите меня, если это выглядит как-то gacked) я могу предоставить больше информации, если это необходимо.
2 ответов
ваш метод 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]];
 }
