How to update a UILabel in Xcode programmatically without XIB files?
Asked Answered
L

2

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.

Liturgist answered 4/4, 2011 at 18:36 Comment(3)
Does the NSLog show the correct location?Jazminejazz
@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 mutableLiturgist
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.Jazminejazz
J
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]];

 }
Jazminejazz answered 4/4, 2011 at 18:40 Comment(4)
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.Liturgist
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.Jazminejazz
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!Liturgist
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.Jazminejazz
C
0

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

Collarbone answered 4/4, 2011 at 18:56 Comment(1)
The compiler will give a warning if it getter and setter are not implemented and not @synthesize is given.Jazminejazz

© 2022 - 2024 — McMap. All rights reserved.