我遇到了显示字符串的标签问题。
所以,基本上,我有一个函数- (void) didFinishUpdatesSuccessfully
。如果正在调用此函数,则表示操作已成功完成。它会创建一个时间戳并将其存储到NSUserDefaults。所有这一切都像一个魅力..
这是功能:
- (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];
}
正在显示带有日期字符串的标签。它会覆盖自己,但似乎保留了前一个字符串,因此它实际上看起来只是在前一个字符串之上添加了另一个层。换句话说,如果我执行- (void) didFinishUpdatesSuccessfully
3次,它会在彼此的顶部显示3个日期字符串,这看起来很糟糕......
我做错了什么?
干杯 扬
答案 0 :(得分:1)
每次调用didFinishUpdatesSuccesfully方法时,您都在重新创建并重新添加UpDLabel。
相反,使UpDLabel成为您班级的属性:
UILabel *UpDLabel
@property (nonatomic, retain) UILabel *upDLabel
在didFinishUpdatesSuccesfully的代码中执行:
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;
答案 1 :(得分:0)
这是因为您不断向视图添加子视图,而不是仅仅将新文本添加到UILabel中。设置UILabel一次,然后只需使用UpDLabel.text = lupdate更改文本。
乔
答案 2 :(得分:0)
为什么不在- viewDidLoad
方法中创建UILabel呢?使标签成为类的属性,然后在- viewDidLoad
上创建标签,然后更新- didFinishUpdatesSuccessfully
内标签内的字符串。它会更方便。