2011-09-23 70 views
1

我希望這是一個簡單的問題,我有一個的UIBarButtonItem,我使用一個UILabel作爲一個自定義視圖初始化,按鈕工具欄的生活裏面。 我想要做的是能夠改變從是的UIBarButtonItem內標籤的文本,這裏是我的代碼:的UIBarButtonItem引用該自定義視圖後編輯

NSDate *lastUpdateDate = [AWSyncEntity getLastUpdatedDateByEntityName:@"Patient" inManagedObjectContext:self.managedObjectContext]; 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];//liberar 
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm a"]; 

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 44.01)]; //liberar 
    myLabel.font = [UIFont boldSystemFontOfSize:10]; 
    myLabel.textColor = [UIColor whiteColor]; 
    myLabel.backgroundColor = [UIColor clearColor]; 
    myLabel.text = [NSString stringWithFormat:@"Actualizado: %@", [dateFormat stringFromDate:lastUpdateDate]]; 

    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:myLabel]; //liberar  
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; //liberar   
    self.toolbarItems = [NSArray arrayWithObjects:flexibleSpace,btn2, flexibleSpace, nil]; 
    [self.navigationController setToolbarHidden:NO animated:YES]; 

    UIBarButtonItem *mybtn = (UIBarButtonItem *)[self.toolbarItems objectAtIndex:2];  
    //I was thinking this would be possible... 
    //UILabel *mylbl = (UILabel *) [mybtn view]; 

    [flexibleSpace release]; 
    [btn2 release]; 
    [myLabel release]; 
    [dateFormat release]; 

我不知道如何再次獲得參考按鈕的內部視圖,任何線索? 我正在考慮做這樣的事情:(但它不工作)。

//I was thinking this would be possible... 
    //UILabel *mylbl = (UILabel *) [mybtn view]; 

回答

0

的標籤是customView欄按鈕的

UILabel *mylbl = (UILabel *)[mybtn customView]; 
0

的UILabel * myLbl =(的UILabel *)[mybtn customView];

應該這樣做,但我沒有測試過它。

+0

謝謝你,我知道這是簡單的,但我沒能找到它,謝謝,雖然它不工作我所期待的方式。我想引用標籤的原因是爲了更新其內容,我希望在工具欄中顯示一個時間戳,並更新了好幾次,但這樣做我最初發布是不是刷新標籤的文字,我可以在唯一的其他方式的方式想到的是創建一個新的按鈕,將其與新的時間戳重新分配到工具欄,你能想到更好的辦法? – user743737

+0

對不起,想不出更好的辦法(我是初學者我自己,一直只有編碼幾個月),但如果我的幫助下,點擊綠色的對勾接受我的答案! –

相關問題