Skip to content

Instantly share code, notes, and snippets.

@jen2
Created May 7, 2017 17:11
Show Gist options
  • Save jen2/098d65fec4ed564266f32e942a83e566 to your computer and use it in GitHub Desktop.
Save jen2/098d65fec4ed564266f32e942a83e566 to your computer and use it in GitHub Desktop.
Code completed before highlighting selected button for "Build a World Clock in Objective-C."
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIButton *nyc;
@property (nonatomic, weak) IBOutlet UIButton *paris;
@property (nonatomic, weak) IBOutlet UIButton *moscow;
@property (nonatomic, weak) IBOutlet UIButton *hongKong;
@property (nonatomic, weak) IBOutlet UIButton *honolulu;
@property (nonatomic, weak) IBOutlet UIButton *seattle;
@property (nonatomic, weak) IBOutlet UILabel *timeLabel;
@property (nonatomic, weak) IBOutlet UILabel *dateLabel;
@property(nonatomic, strong)NSString *timeZone;
@property(nonatomic, strong)UIButton *selectedButton;
@property(nonatomic, strong)NSTimer * timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.nyc sendActionsForControlEvents: UIControlEventTouchUpInside];
}
- (IBAction)cityButtonTapped:(UIButton *)sender {
switch (sender.tag) {
case 100:
self.timeZone = @"EDT";
self.selectedButton = self.nyc;
break;
case 101:
self.timeZone = @"CEST";
self.selectedButton = self.paris;
break;
case 102:
self.timeZone = @"MSD";
self.selectedButton = self.moscow;
break;
case 103:
self.timeZone = @"HKT";
self.selectedButton = self.hongKong;
break;
case 104:
self.timeZone = @"HST";
self.selectedButton = self.honolulu;
break;
case 105:
self.timeZone = @"PST";
self.selectedButton = self.seattle;
break;
default:
self.timeZone = @"EDT";
self.selectedButton = self.nyc;
break;
}
[self setTappedCityTimer];
}
- (void)setTappedCityTimer {
[self.timer invalidate];
self.timer = nil;
[self setDateTimeLabelsWithTimeZone];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setDateTimeLabelsWithTimeZone) userInfo:nil repeats:YES];
}
- (void)setDateTimeLabelsWithTimeZone {
NSArray *dateAndTime = [self formatCurrentDateTimeForTimeZone];
self.timeLabel.text = dateAndTime[1];
self.dateLabel.text = dateAndTime[0];
}
- (NSArray *)formatCurrentDateTimeForTimeZone {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSTimeZone *localTimeZone = [NSTimeZone timeZoneWithAbbreviation:self.timeZone];
[dateFormatter setLocale:posix];
[dateFormatter setDateFormat:@"EEEE MMMM dd y"];
[dateFormatter setTimeZone:localTimeZone];
[timeFormatter setLocale:posix];
[timeFormatter setDateFormat:@"h:mm:ss a"];
[timeFormatter setTimeZone:localTimeZone];
NSDate *now = [NSDate date];
NSString *date = [dateFormatter stringFromDate:now];
NSString *time = [timeFormatter stringFromDate:now];
NSArray *formattedDateAndTime = @[date, time];
return formattedDateAndTime;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment