Reputation: 4302
I am creating a UILabel in a method called displayTemp
- (UILabel *) displayTemp
{
_tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 30)];
[self.view addSubview:_tempLabel];
NSDictionary *key = [self.getAPICall objectForKey:@"currently"];
_tempLabel.text = [key objectForKey:@"temperature"];
return _tempLabel;
}
This is simply just bringing back a value from an API call.
I then want to show dis UILabel and its text in the viewDidLoad method
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor colorWithRed:0.976 green:0.518 blue:0.439 alpha:1];
UILabel *getTemp = self.displayTemp;
//How do I return the text property of self.DisplayTemp
}
How would I then return this? Is there a better way of doing this?
Upvotes: 0
Views: 67
Reputation: 2762
In addition @MichaelDautermann's answer, I suggest you to create UILabel just one time in -(UILabel *)displayTemp
method with conditional branch(if). Although the method would be called just one time since it is called by -(void)viewDidLoad
, in terms of architecture of class, you would better make the method more flexible and safe against multiple calls.
Therefore, I modified the method as follow:
- (UILabel *) displayTemp
{
if (_tempLabel == nil) {
_tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 30)];
NSDictionary *key = [self.getAPICall objectForKey:@"currently"];
_tempLabel.text = [key objectForKey:@"temperature"];
[self.view addSubview:_tempLabel];
}
return _tempLabel;
}
I hope that my suggestion will be useful for you.
Upvotes: 0
Reputation: 89509
You're mixing idioms here. Instead of doing this "@property
" type thing:
UILabel *getTemp = self.displayTemp;
Change that line to this:
[self displayTemp];
in your "viewDidLoad
" method and you'll be okay. You don't need to return a UILabel object from the displayTemp method because you're already adding it to your view controller's view.
Upvotes: 1