I have a datePickerView which I want it to have a min date = current time, max date = 48hrs later. It's currently working fine, as I can't pick out of that range. But there's some aesthetic problems. Some of the period in that range is not in black. For example in the picture below, today's 7hour hand is suppose to be in black but its not.
- (void)viewDidLoad
{
[super viewDidLoad];
NSDate *now = [[NSDate alloc] init];
NSLog(#"now is %#", now);
[datepick setDate:now animated:YES];
[now release];
datepick.minimumDate = now;
NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setHour:48];
NSDate *targetDate = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate options:0];
[dateComponents release];
[gregorian release];
datepick.maximumDate = targetDate;
NSLog(#"targetDate is %#", targetDate);
}
This is default behavior when you set a minimum date and a maximum date and APPLE does it clearly to make it obvious that they are not selectable.
What you can do is implement a UIPickerView for the dates and implement viewForRow for the pickerView methods.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Add label.text which is the picker value for the row (for that component)
// set the font for the label as black.
}
You can also change the font size of the values in the pickerView.
Related
I'm making an iOS application for iPhone, and I want to have two labels that is hidden on a specific time and shown on a specific time. Like Label1 is shown between 6am and 6pm and Label2 is shown between 6pm and 6am
Any ideas?
[NSDate date]; gives you the current date .Use date fromatter to get the fromat you need and use setText: method to set the text.Fire it in a timer and set the text
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEEE MMMM d, YYYY"];
NSLog(#"%#",[dateFormatter stringFromDate:[NSDate date]]);
[yourLabel setText:[dateFormatter stringFromDate:[NSDate date]]];
EDIT
full code,Try this
-(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;
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];
Now you have the hour, just add your normal logic.
if want to be change the formate do this...
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
// another is [formatter setDateFormat:#"hh:mm:ss"];
NSLog(#"Current Date: %#", [formatter stringFromDate:today]);
I have a UITextfield "Birthday" and right now after clicking on the text field I have a keyboard and I would like to change it to a datepicker.
Is there anyone who can help step by step with this?what should I add to my code and what changes do I need to make.
Here is what I have for my UITextField "Birthady":
field = [[UITextField alloc] initWithFrame:frame];
[self.appDel.styleManager decorateTextField:field];
field.placeholder = #"Birthday";
[container addSubview:field];
self.registrationFormBirthday = field;
self.registrationFormBirthday.delegate = self;
Based on Jonathan answer this is what I have right now:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if textField == XtextLabel
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init] ;
[comps setYear:-0];
NSDate *maximumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-90];
NSDate *minimumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maximumDate];
[datePicker setMinimumDate:minimumDate];
datePicker.datePickerMode = UIDatePickerModeDate;
textField.inputView = datePicker;
}
}
How Can I show the selected date on in the XtextLabel and exit datePicker?
Something like...
UIDatePicker *datePicker = [[[UIDatePicker alloc] init]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
textField.inputView = datePicker;
- (IBAction)datePickerValueChanged:(id)sender {
NSDate *pickerDate = [sender date]; //Do something with the date
}
I have a date picker view controller where I select the date. When I come back I can see the selected date on the tableview cell. So far, this all works properly. I face a problem when I exit the application and run it again, I can't see the last selected value in the cell. How can I do this?
If the user exits the application, they should be able to see the last selected date on the cell so that they can review or use that date.
This is my date controller class code .m file
- (void)viewDidLoad
{
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minimumDate = [NSDate date];
[self.view addSubview:datePicker];
[super viewDidLoad];
}
-(void)DateChangeForFinalPayMent
{
//remove time
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int comps = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:comps fromDate:[datePicker date]];
NSDate *dateSelected = [gregorian dateFromComponents:dateComponents];
NSLog(#"dateSelected:%#",dateSelected);
[gregorian release];
if ([selectionData type] == 0)
[selectionData setFromDateSelected:dateSelected];
else
[selectionData setToDateSelected:dateSelected];
}
-(IBAction)click
{
[self DateChangeForFinalPayMent];
[self.navigationController popViewControllerAnimated:YES];
}
here is my firstview controller where i show my selected value date on cell
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString* fromDate = [dateFormat stringFromDate:app.selectionData.fromDateSelected];
NSString* toDate = [dateFormat stringFromDate:app.selectionData.toDateSelected];
if ([indexPath row] == 0 && [indexPath section] == 1)
{
cell.textLabel.text=#"From Date";
cell.detailTextLabel.text=fromDate;
}
if ([indexPath row] == 1 && [indexPath section] == 1)
{
cell.textLabel.text=#"To Date";
cell.detailTextLabel.text=toDate;
}
Can anybody suggest how to store the value and show it to the user when they open the application again.
PFB code to save and reteive the date value from the User defaults
//Storing date value to the user defaults
NSDate * selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the date format.. u can choose different one based on ur requirement
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//String from the NSDate
NSString *pStrDate = [dateFormatter stringFromDate:selectedDate];
//Save date value to the user defaults
[[NSUserDefaults standardUserDefaults] setObject: pStrDate forKey:#"date"];
[[NSUserDefaults standardUserDefaults] synchronize];
Retrieving date from the user defaults:
NSDate *dateFromString = [[NSDate alloc] init];
NSString *savedDateVaue=nil;
if([[NSUserDefaults standardUserDefaults] objectForKey:#"date"]!=nil)
{
savedDateVaue=[[NSUserDefaults standardUserDefaults] objectForKey:#"date"];
dateFromString = [dateFormatter dateFromString:savedDateVaue];
}
hi friend i have to make date application
i have groped tableview.i have one section and two row
when my application start i am showing default date on both cell on first cell i was showing today day+1 and on nextcell i am showing today day+6 they i can see but
- (id)init
{
[super initWithStyle:UITableViewStyleGrouped];
NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:1];
app.selectionData.fromDateSelected = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate options:0];
[dateComponents release];
[gregorian release];
//this is for 6day
NSDate *todaysDate1 = [NSDate date];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents1 = [[NSDateComponents alloc] init];
[dateComponents1 setDay:6];
app.selectionData.toDateSelected = [gregorian1 dateByAddingComponents:dateComponents1 toDate:todaysDate1 options:0];
[dateComponents1 release];
[gregorian1 release];
return self;
}
but know i want if
the first cell selected date is changed from date pickerview and second cell date is less than the first cell selected date then change the second cell date automatically to first cell selected date+5 please help me how to solve it what the change i have to do in my code wher i am show by default this for once time on this this is my code on run time
this code i writemin my firstview controller class where i am show this date when my application start
in tableviewcellrowAtindexpath
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString*fromDate = [dateFormat stringFromDate:app.selectionData.fromDateSelected];
// NSString *toDate = [dateFormat stringFromDate:targetDate1];
NSString *toDate = [dateFormat stringFromDate:app.selectionData.toDateSelected];
if ([indexPath row] == 0 && [indexPath section] == 1)
{
cell.textLabel.text=#"From Date";
cell.detailTextLabel.text=fromDate;
NSLog(#"this is for fromDate:%#",fromDate);
}
if ([indexPath row] == 1 && [indexPath section] == 1)
{
cell.textLabel.text=#"To Date";
cell.detailTextLabel.text=toDate;
}
[dateFormat release];
dateFormat = nil;
this is code od dateview where i put my datepicker from here i selected date
- (void)viewDidLoad
{
// if(datePicker != nil && [datePicker retainCount] > 0)
// {
// [datePicker release];
//
// datePicker = nil;
//}
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
[datePicker addTarget:self action:#selector(DateChangeForFinalPayMent) forControlEvents:UIControlEventValueChanged];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.date = [NSDate date];
datePicker.minimumDate = [NSDate date];
[self.view addSubview:datePicker];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)DateChangeForFinalPayMent{
if ([selectionData type] == 0)
[selectionData setFromDateSelected:[datePicker date]];
else
[selectionData setToDateSelected:[datePicker date]];
// [super viewWillDisappear:animated];
}
I wrote an example project for you that does this
Basically - look at the delegate method of the picker view controller.
It sends back a new date - and from that date you can calculate the second date and reload your table view.
This is the code to get date from datepicker. 1st select the date & then select the cell you need to update.
NSString *dateString = nil;
-(void)DateChangeForFinalPayMent:(id)sender {
NSLocale *usLocale = [[[NSLocale alloc]initWithLocaleIdentifier:#"en_US"] autorelease];
NSDate *pickerDate = [self.datePicker date];
NSString *selectionString = [[NSString alloc] initWithFormat:#"%#", [pickerDate descriptionWithLocale:usLocale]];
NSLog(#"string =%#",selectionString);
dateString = selectionString;
[selectionString release];
}
On selecting the cell Perform this operation
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath.row;
NSArray *array = [NSArray arrayWithObject:indexPath];
[tablview reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//some condition over here
if(selectedIndex == indexPath.row)
cell.textLabel = dateString;
//some condition over her
}
You use this code according to your implementation..
The following code is to show my UIDatepicker:
- (IBAction)showActionSheet:(id)sender {
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? #"\n\n\n\n\n\n\n\n\n" : #"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:#"%#%#", title, NSLocalizedString(#"Please Select A Date", #"")]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"I've Made My Choice", nil];
[actionSheet showInView:self.view];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];
}
It all works properly, but what I'd like to do is to display the users choice into a label which I have named " label1 ". Can anyone help? Thanks!
UPDATE:
Here is the updated code:
- (IBAction)showActionSheet:(id)sender {
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? #"\n\n\n\n\n\n\n\n\n" : #"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:#"%#%#", title, NSLocalizedString(#"Please Select A Date", #"")]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"I've Made My Choice", nil];
[actionSheet showInView:self.view];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];
[datePicker addTarget:self
action:#selector(updateLabel:)
forControlEvents:UIControlEventValueChanged];
}
- (void)updateLabel:(id)sender {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label1.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
[df release];
/* the following allows you to choose the date components
NSCalendar *calendar = [NSCalendar currentCalendar];
int hour = [[calendar components:NSHourCalendarUnit fromDate:[datePicker date]] hour];
int minute = [[calendar components:NSMinuteCalendarUnit fromDate:[datePicker date]] minute];
int year = [[calendar components:NSYearCalendarUnit fromDate:[datePicker date]] year];
int month = [[calendar components:NSMonthCalendarUnit fromDate:[datePicker date]] month];
int day = [[calendar components:NSDayCalendarUnit fromDate:[datePicker date]] day];
*/
}
That's for the .m , my .xib has the 'label' tied properly too :) just getting a (null) value whenever I choose my date :(
.h
#import <UIKit/UIKit.h>
#interface YourViewController : UIViewController {
IBOutlet UILabel *label1;
}
#property (nonatomic, retain) UILabel *label1;
#end
.m
#implementation YourViewController
#synthesize label1;
/*
All of your code goes here
*/
#end
Interface Builder
Also add the following to your showActionSheet:
[datePicker addTarget:self
action:#selector(updateLabel:)
forControlEvents:UIControlEventValueChanged];
This should update your label from datePicker:
-(void)updateLabel:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label1.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
[df release];
/* the following allows you to choose the date components
NSCalendar *calendar = [NSCalendar currentCalendar];
int hour = [[calendar components:NSHourCalendarUnit fromDate:[datePicker date]] hour];
int minute = [[calendar components:NSMinuteCalendarUnit fromDate:[datePicker date]] minute];
int year = [[calendar components:NSYearCalendarUnit fromDate:[datePicker date]] year];
int month = [[calendar components:NSMonthCalendarUnit fromDate:[datePicker date]] month];
int day = [[calendar components:NSDayCalendarUnit fromDate:[datePicker date]] day];
*/
}
Here I shows that UIDatePicker in UITableview cell.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,185,0,0)];
[self.view addSubview:pv];
return NO;
}
If you are using UITableView, then use following in order to show UIDatePicker instead of keyboard.
if (0 == indexPath.row) {
cell.rightTextField.placeholder = #”Date”;
cell.rightTextField.text = [coAccident strDateTime];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 185, 0)];
[datePicker addTarget:self action:#selector(changeDateInLabel :) forControlEvents:UIControlEventValueChanged];
cell.rightTextField.inputView = datePicker;
}
- (IBAction)changeDateInLabel:(id)sender {
UIDatePicker * datePicker = (UIDatePicker*)sender;
CAccidentVO* coAccident = [m_arrVehicles objectAtIndex:1];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[coAccident setStrDateTime:[NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]]];
[df release];
[m_tvVehicleDetails reloadData];
}