2011-08-18 112 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