我有一个按钮,按下按钮在时区之间循环并显示它们的时间;我不得不添加一个NSTimer,所以时间会在视图上更新。问题是当我按下按钮时,显示第二个时区时间,然后在一秒后消失,并被第一个时区替换。我认为这与NSTimer有关,因为当我评论它时,按钮工作正常,但时间不更新。任何帮助,将不胜感激。这是我的代码:
创建按钮
UIButton *changeZone = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
changeZone.center = CGPointMake(250, 515);
[changeZone addTarget:self action:@selector(changeZone:) forControlEvents:UIControlEventTouchUpInside];
[changeZone setTitle:@"" forState:UIControlStateNormal];
[self.view addSubview:changeZone];
设置基本TimeZone和NSTimer
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"hh:mm"];
self.zoneName.text = (@"NYC");
NSString *strSelectedDate= [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
添加按钮操作并配置NSTimer
- (void)changeZone:(UIButton *) sender {
if (_isTimeZone) {
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"hh:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
self.zoneName.text = (@"NYC");
NSString *strSelectedDate = [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
_isTimeZone = NO;
} else {
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"hh:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
self.zoneName.text = (@"LON");
NSString *strSelectedDate = [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
_isTimeZone = YES;
}
}
- (void)updateTime:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"hh:mm"];
NSString *strSelectedDate = [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
});
}
答案 0 :(得分:2)
您在计时器方法中创建的格式化程序,对您使用按钮设置的时区一无所知,因为您正在创建新的格式化程序。使用ivar而不是创建一个新的,只改变一个不同的东西,即时区。
@interface ViewController ()
@property (weak,nonatomic) IBOutlet UILabel *zone;
@property (weak,nonatomic) IBOutlet UILabel *zoneName;
@property (nonatomic) BOOL isTimeZone;
@end
@implementation ViewController {
NSDateFormatter *formatter;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *changeZone = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
changeZone.center = CGPointMake(250, 515);
[changeZone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:changeZone];
[changeZone addTarget:self action:@selector(changeZone:) forControlEvents:UIControlEventTouchUpInside];
[changeZone setTitle:@"Change Zone" forState:UIControlStateNormal];
formatter = [NSDateFormatter new];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"hh:mm"];
NSDate *date = [NSDate date];
self.zoneName.text = (@"NYC");
NSString *strSelectedDate= [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
NSLog(@"%@",self.zoneName);
}
- (void)changeZone:(UIButton *) sender {
if (_isTimeZone) {
NSDate *date = [NSDate date];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
self.zoneName.text = (@"NYC");
NSString *strSelectedDate = [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
_isTimeZone = NO;
} else {
NSDate *date = [NSDate date];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
self.zoneName.text = (@"LON");
NSString *strSelectedDate = [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
_isTimeZone = YES;
}
}
- (void)updateTime:(NSTimer *) timer {
NSDate *date = [NSDate date];
NSString *strSelectedDate = [formatter stringFromDate:date];
self.zone.text = strSelectedDate;
}