2013-04-02 19 views
0

我試圖添加計時器到UINavigationTabBar使用viewForHeaderInSection:。 但它不顯示時間。如果我在 - (void)viewDidLoad方法中嘗試相同的工作。我的代碼有什麼問題,是否有可能在NavigationTabBar中調用NSDateFormatter?下面是代碼將NSDateFormatter設置爲HeaderSection

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)]; 
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)]; 
    label1.backgroundColor=[UIColor blueColor]; 
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; 
    [dateFormatter setDateFormat:@"hh:mm:ss"]; 
    label1.text=[dateFormatter stringFromDate:[NSDate date]]; 
    [self performSelector:@selector(timeLimiteAlert) withObject:self afterDelay:1.0]; 
    [view addSubview:label1]; 
    [label1 release]; 
    return [view autorelease]; 
} 
+0

ViewForHeaderInsection委託方法僅用於在tableviews中設置自定義節標題。請更好地解釋你想達到的目標。 –

回答

0

你忘了將視圖添加到導航欄:

self.navigationItem.titleView = view; 

因此,代碼會是這樣:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)]; 
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)]; 
label1.backgroundColor=[UIColor blueColor]; 
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"hh:mm:ss"]; 
label1.text=[dateFormatter stringFromDate:[NSDate date]]; 
[self performSelector:@selector(timeLimiteAlert) withObject:self afterDelay:1.0]; 
[view addSubview:label1]; 
self.navigationItem.titleView = view; 

並把它在viewDidLoad中。 ;)

+0

在你已經在你的問題中寫下這段代碼之前,爲什麼你告訴我我忘了self.navigationItem.titleView = view; – Sovannarith

+0

主題首發忘記了代碼。不是你。 –

+0

如果它幫助你,請給這個答案+1! –

0

請嘗試在viewDidLoad中進行設置:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)]; 
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)]; 
label1.backgroundColor=[UIColor blueColor]; 
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; 

希望工程。