Skip to content

Instantly share code, notes, and snippets.

@pmatanyc
Created December 14, 2013 22:22
Show Gist options
  • Save pmatanyc/7965715 to your computer and use it in GitHub Desktop.
Save pmatanyc/7965715 to your computer and use it in GitHub Desktop.
Day of Week App - displays day of week for user specified date (UIDatePicker)
// PMMainViewController.h
// SampleProject
//
// Created by Paola Mata on 12/6/13.
//
//
#import <UIKit/UIKit.h>
@interface PMMainViewController : UIViewController
{
UILabel *dateLabel;
UIDatePicker *datePicker;
UILabel *dayOfWeekLabel;
UILabel *dayResponseLabel;
NSString *weekday;
NSDate *datePicked;
}
@end
//
// PMMainViewController.m
// SampleProject
//
// Created by Paola Mata on 12/6/13.
//
//
#import "PMMainViewController.h"
@interface PMMainViewController ()
@end
@implementation PMMainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"Find Day of the Week";
dateLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 320, 35)];
dateLabel.textColor = [UIColor grayColor];
dateLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:dateLabel];
dateLabel.text=@"Select a Date";
datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 100, 0, 0)];
datePicker.datePickerMode = UIDatePickerModeDate;
[self.view addSubview:datePicker];
datePicked = [[NSDate alloc] init];
[datePicker addTarget:self
action:@selector(dateSelected:)
forControlEvents:UIControlEventValueChanged];
dayOfWeekLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, 320, 35)];
dayOfWeekLabel.textColor = [UIColor grayColor];
dayOfWeekLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:dayOfWeekLabel];
dayOfWeekLabel.text = @"Day of the Week";
dayResponseLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 380, 320, 35)];
dayResponseLabel.textAlignment= NSTextAlignmentCenter;
dayResponseLabel.textColor = [UIColor lightGrayColor];
dayResponseLabel.text= @"will display here";
[self.view addSubview:dayResponseLabel];
}
-(IBAction)dateSelected:(id)sender
{
datePicked = datePicker.date;
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:datePicked];
int weekdayUnit = [dateComponents weekday];
NSArray *daysOfWeek = [[NSArray alloc] initWithObjects: @"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];
weekday = [daysOfWeek objectAtIndex:weekdayUnit-1];
dayResponseLabel.textColor = [UIColor blueColor];
dayResponseLabel.text = weekday;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment