0

我尝试使用viewForHeaderInSection:将计时器添加到UINavigationTabBar。但它不显示时间。如果我在 -(void)viewDidLoad 方法中尝试相同的方法,它正在工作。我的代码有什么问题。是否可以在 NavigationTabBar 中调用 NSDateFormatter?这是代码

- (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];
}
4

2 回答 2

0

您忘记将视图添加到 NavigationBar:

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 中。;)

于 2013-04-02T10:30:44.867 回答
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];

希望它有效。

于 2013-04-02T10:30:53.193 回答