2011-08-18 102 views
0

我遇到了显示字符串的标签问题。字符串/标签问题

所以,基本上,我有一个功能- (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]; 

    } 

正在显示的日期字符串的标签。它覆盖了自己,但似乎保留了以前的字符串,所以它看起来好像只是在前一个字符串上添加了另一个图层。或者换句话说,如果我执行3次- (void) didFinishUpdatesSuccessfully它会显示3个日期字符串在彼此之上看起来被拧紧...

我在做什么错了?

干杯 月

回答

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; 
+0

这完美的作品。非常感谢。 – Jan

+1

注意:我已将代码添加到我的viewDidLoad中。现在完美运作。再次感谢。 – Jan

0

这是因为你不断添加子视图您的视图,而不是仅仅把新文成的UILabel。设置一次UILabel,然后用UpDLabel.text = lupdate更改文本。

0

为什么不创建的UILabel的- viewDidLoad方法,而不是里面?使标签成为该类的一个属性,然后在- viewDidLoad上创建标签,然后更新- didFinishUpdatesSuccessfully内部标签内的字符串。它会更方便。

+0

好主意!谢谢 – Jan