9

I'm stuck :(
In my application I require an update from CLLocationManager every time it gets an update to a new position. I am not using XIB/NIB files, everything I coded I have done programmatically. To the code:
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]];

}
...

Any ideas, or help?

(this was all hand jammed so please forgive me if it looks kinda gacked) I can provide more info if needed.

dcrawkstar
  • 505
  • 1
  • 5
  • 11
  • Does the NSLog show the correct location? – GorillaPatch Apr 04 '11 at 18:41
  • @Radek I put it in the loadView, should the UILabel be put into viewDidLoad? Is that the override point? And why would that make the UILabel mutable – dcrawkstar Apr 04 '11 at 19:00
  • Using loadView in a UIViewController to programmatically create a user interface is OK. But you are right if you want to refer to IBOutlets you would use the viewDidLoad method to ensure the XIB has been loaded and all outlets are already available. – GorillaPatch Apr 04 '11 at 19:00

2 Answers2

16

Your loadView method is wrong. You do not set the instance variable properly but instead you generate a new local variable. Change it to the following by omitting the UILabel * and do not release it because you want to keep a reference around to the label to set the text later.

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

Then later directly access the variable like this:

 - (void)locationManager:(CLLocationManager *)manager
     didUpdateToLocation:(CLLocation *)newLocation
            fromLocation:(CLLocation *)oldLocation
 {
     NSLog(@"Location: %@", [newLocation description]);

     theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]];

 }
GorillaPatch
  • 5,007
  • 1
  • 39
  • 56
  • I still get the original text "this is some text", it never updates, even though in I can see the NSLog output in the console printed with a new location. – dcrawkstar Apr 04 '11 at 18:48
  • I updated the answer. I think your theLabel instance variable which you reference to in the location manager delegate callback is still nil because you have to properly assigned it. – GorillaPatch Apr 04 '11 at 18:55
  • you were right, it needed to be a local var... you might change the above code and remove the [theLabel release] with comment, as that would release too early (ie a typo in the answer) Thanks, you rock! – dcrawkstar Apr 05 '11 at 01:35
  • Right, I wrote in bold not to release the ivar and I copied your code and released it. How embarrassing... I corrected the code. You are very welcome. – GorillaPatch Apr 10 '11 at 08:01
0

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