我正在为iPhone制作iOS应用程序,我希望在特定时间隐藏两个标签并在特定时间显示。类似于Label1显示在早上6点到下午6点之间,Label2显示在下午6点到早上6点之间 有什么想法吗?
答案 0 :(得分:1)
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
现在你有了时间,只需添加你的正常逻辑。 如果想要改变甲酸酯做这个......
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
// another is [formatter setDateFormat:@"hh:mm:ss"];
NSLog(@"Current Date: %@", [formatter stringFromDate:today]);
答案 1 :(得分:1)
[NSDate date];
为您提供当前日期。使用日期来获取您需要的文件并使用setText:
方法设置文本。将其放入计时器并设置文本
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE MMMM d, YYYY"];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);
[yourLabel setText:[dateFormatter stringFromDate:[NSDate date]]];
修改
完整代码,试试这个
-(void)showtext
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mm:ss"];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);
if( [self compareTimeIsbetween6])
{
[self.date1Label setHidden:NO];
[self.date2Label setHidden:YES];
[self.date1Label setText:[dateFormatter stringFromDate:[NSDate date]]];
}
else
{
[self.date1Label setHidden:YES];
[self.date2Label setHidden:NO];
[self.date2Label setText:[dateFormatter stringFromDate:[NSDate date]]];
}
}
-(BOOL)compareTimeIsbetween6
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger currentHour = [components hour];
NSInteger currentMinute = [components minute];
NSInteger currentSecond = [components second];
if (currentHour < 6 || (currentHour > 18 || (currentHour == 18 && (currentMinute > 0 || currentSecond > 0)))) {
return YES;
}
else
{
return NO;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showtext) userInfo:nil repeats:YES]; // Do any additional setup after loading the view, typically from a nib.
}
·H
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *date1Label;
@property (weak, nonatomic) IBOutlet UILabel *date2Label;