String / label issue
I've got an issue with a label showing a String.
So, basically, I've got a function - (void) didFinishUpdatesSuccessfully
. If this function is being called it means that an action has been completed successfully. It creates a time stamp and stores it to NSUserDefaults. All this works like a charme..
Here's the function:
- (void) didFinishUpdatesSuccessfully {
// Create formatted date string
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY - hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSMutableString* lastUpdated;
lastUpdated = [[NSMutableString stringWithFormat:@"%@",dateString] retain];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:lastUpdated forKey:@"lastUpdated"];
[defaults synchronize];
//--- Alert dialog
NSString *title;
title = @"All the latest hotspots have now been saved!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
message: lastUpdated
delegate: nil
cancelButtonTitle: [FileUtils appResourceForKey:@"HOTSPOT_GENERAL_BUTTON_TITLE_OK"]
otherButtonTitles: nil];
// ------------------------
//Create label
UILabel *UpDLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 205, 250, 100)];
UpDLabel.backgroundColor = [UIColor clearColor]; // [UIColor brownColor];
UpDLabel.font = [UIFont fontWithName:@"Helvetica" size: 11.0];
UpDLabel.shadowColor = [UIColor whiteColor];
UpDLabel.shadowOffset = CGSizeMake(1,1);
UpDLabel.textColor = [UIColor blackColor];
UpDLabel.textAlignment = UITextAlignmentRight; // UITextAlignmentCenter, UITextAlignmentLeft
UpDLabel.lineBreakMode = UILineBreakModeWordWrap;
NSString *lupdate = [NSString stringWithFormat: @"Last Update: %@", lastUpdated];
UpDLabel.text = lupdate;
[self.view addSubview:UpDLabel];
[UpDLabel release];
// ------------------------
[dateFormatter release];
[lastUpdated release];
[alert show];
[alert release];
//--- remove indicator view
[self indicatorViewShow:NO];
}
The label with the date string is being displayed. It overrides itself but seems to keep the previous string so it actualy looks like it just adds another layer on top of the previous on开发者_运维知识库e. Or in other words, if I perform the - (void) didFinishUpdatesSuccessfully
3 times it will show 3 date strings on top of each other which looks screwed...
What am I doing wrong?
Cheers Jan
You are recreating and re-adding the UpDLabel to your view every time the didFinishUpdatesSuccesfully method is called.
Instead make UpDLabel a property of your class:
UILabel *UpDLabel
@property (nonatomic, retain) UILabel *upDLabel
And in your code in the didFinishUpdatesSuccesfully do:
if (!self.upDLabel) {
self.upDLabel = [[[UILabel alloc] initWithFrame:CGRectMake(50, 205, 250, 100)] autorelease];
self.upDLabel.backgroundColor = [UIColor clearColor]; // [UIColor brownColor];
self.upDLabel.font = [UIFont fontWithName:@"Helvetica" size: 11.0];
self.upDLabel.shadowColor = [UIColor whiteColor];
self.upDLabel.shadowOffset = CGSizeMake(1,1);
self.upDLabel.textColor = [UIColor blackColor];
self.upDLabel.textAlignment = UITextAlignmentRight; // UITextAlignmentCenter, UITextAlignmentLeft
self.upDLabel.lineBreakMode = UILineBreakModeWordWrap;
[self.view addSubview:self.upDLabel];
}
NSString *lupdate = [NSString stringWithFormat: @"Last Update: %@", lastUpdated];
self.upDLabel.text = lupdate;
It's because you keep adding subviews to your view, instead of just putting new text into the UILabel. Set up the UILabel once, and then just change the text with UpDLabel.text = lupdate.
joe
Why not create the UILabel inside the - viewDidLoad
method instead? Make the label a property of the class then create the label on - viewDidLoad
then update the string inside the label inside the - didFinishUpdatesSuccessfully
. It will be more convenient.
精彩评论