how to call date on cell selected from datepickerview in iphone - iphone

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..

Related

UIAlertView button is disabled even after picking date from UIDatePicker in iOS 7

The problem is that after picking date from UIDatePicker the "Ok" button of UIAlertView is disabled.I tried ,but no luck.
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
should be called whenever we type anything in textfield,but when date is entered it's not getting called,but if i type anything from keyboard,its ok then.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0 && indexPath.row == 1)
{
alertView1 = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done",nil];
alertView1.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
alertView1.tag=1;
alertText = [alertView1 textFieldAtIndex:0];
itemText = [alertView1 textFieldAtIndex:1];
alertText.inputView=datePicker;
itemText.inputView=secondPicker;
[datePicker addTarget:self action:#selector(firstTF) forControlEvents:UIControlEventValueChanged];
[secondPicker addTarget:self action:#selector(secondTF) forControlEvents:UIControlEventValueChanged];
[alertText setPlaceholder:#"From Date"];
[itemText setPlaceholder:#"To Date"];
itemText.secureTextEntry = NO;
[alertView1 show];
}
- (void)firstTF
{
NSDate *date = datePicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
alertText.text = [dateFormat stringFromDate:date];
}
- (void)secondTF
{
NSDate *date = secondPicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
itemText.text = [dateFormat stringFromDate:date];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] > 0)
{
NSLog(#"alertViewShouldEnableFirstOtherButton: was called!");
return YES;
}
else
{
return NO;
}
}
The problem is you are not calling it to validate again..After values get inserted you should write this line to call validation..
[alertText sendActionsForControlEvents:UIControlEventEditingChanged];
in this method of yours..
- (void)firstTF

how to store selected value and show on cell when application run again in iphone

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];
}

UI Datepicker range. iPhone

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.

Calendar API changing

http://code.google.com/p/scm-subversion/source/browse/trunk/iPhone/CalendarTest/?r=4#CalendarTest%253Fstate%253Dclosed
I am using the above mentioned Calendar API. In CalendarTestViewController.m class we are getting the date. Now I have declared a global variable nsstring type and I want to use that date into another new class by using global variable. I have tried this a number of times but unable to get the output. If anyone know how can I use the selected date by using Calendar API, then please give me some solution.
Thanks in advance.
CODE
- (void)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile{
NSLog(#"Date Selected is %#",[aTile date]);
str=(NSString *)[aTile date];
}
NSLog(#"str:%#",str);
glbdate1 = (NSString *)[aTile date];
NSLog(#"glbdate1:%#",glbdate1);
}
//I have declared the glbdate1 variable globally in app delegate file and i have made the new class calenderview
//I want to display the date in textfield by using global variable. Here is code in calenderview
-(void)viewWillAppear:(BOOL)animated {
if ([glbdate1 length] != 0)
{
from.text = glbdate1;
}
}
You have to convert the NSDate to NSString as follows,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
NSString *dateString=[dateFormatter stringFromDate:date];
Then the same NSString value can be converted to NSDate as,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
NSDate *dateFromString=[dateFormatter dateFromString:dateString];
-(BOOL)isDateinEventArray:(NSString*)iDateString{
//NSString *searchText = lblDate.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (ToDo *todoObj in appDelegate.todoArray)
{
NSString *date = todoObj.startDate;
[searchArray addObject:date];
}
if ([searchArray count] > 0) {
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:iDateString
options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
if ([copyListOfItems count] > 0) {
return TRUE;
}else{
return FALSE;
}
}
/*----- Calendar Delegates -----> */
- (void)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile{
NSLog(#"Date Selected is %#",[aTile date]);
// NSString *stringFromDate =
// [[NSString alloc]initWithString:[NSString stringWithFormat:#"%02i-%02i-%i",[aTile.date dayOfMonth],
//
[aTile.date monthOfYear],[aTile.date yearOfCommonEra]]];
NSString *stringFromDate = [[NSString alloc]initWithString:[NSString stringWithFormat:#"%02i-%02i-%02i",[aTile.date yearOfCommonEra],
[aTile.date monthOfYear],[aTile.date dayOfMonth]]];
if([self isDateinEventArray:stringFromDate]){
selectedDate = [aTile date];
eventFoundMsg = #"Events are found on this date.";
eventMsgTag = 1;
}else{
eventFoundMsg = #"No events are found on this date.";
eventMsgTag = 0;
}
[stringFromDate release];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Event Message"
message:eventFoundMsg
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil]autorelease];
alert.tag = eventMsgTag;
[alert show];
[aTile flash];
/*
if(tile == nil)
tile = aTile;
else
[tile restoreBackgroundColor];
*/
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (eventMsgTag == 1) {
if (buttonIndex == 0)
{
MyToDoView *detailViewController = [[MyToDoView alloc]
initWithNibName:#"MyToDoView" bundle:nil];
detailViewController.searchDate = selectedDate;
NSLog([NSString stringWithFormat:#"%#",detailViewController.searchDate]);
[detailViewController searchDateTableView];
[self.navigationController popViewControllerAnimated:YES];
[detailViewController release];
}
}
}
I hope this can help you....

NSDate - Change Time Component

This is probably a silly question but I can't seem to find an answer.
I am using a date picker to allow a user to enter a date and it is also setting the time to the current time (I init the picker with [NSDate date]).
Is there a way to change the time component of the date? I am later calculating time intervals which returns seconds and I am getting some odd behavior because of the time.
This code shows the date entry:
#implementation AddEventViewController
#synthesize backgroundColor, nameField, dateField, datePicker, eventName, eventDate;
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = self.backgroundColor;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
CGSize size = self.view.bounds.size;
CGRect frame = CGRectMake(0, size.height - 216, 320, 216);
datePicker = [[UIDatePicker alloc] initWithFrame: frame];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = YES;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:#selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview: datePicker];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)doneButton
{
if ((nameField.text != nil))
{
self.eventName = nameField.text;
self.eventDate = datePicker.date;
[[NSNotificationCenter defaultCenter]postNotificationName:#"EventAddDone" object:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Enter Event Name" message:nil delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
-(IBAction)cancelButton
{
[[NSNotificationCenter defaultCenter]postNotificationName:#"EventAddCancel" object:nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)changeDateInLabel:(id)sender
{
dateField.text = [dateFormatter stringFromDate:[datePicker date]];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == dateField)
{
dateField.placeholder = #" ";
dateField.text = [dateFormatter stringFromDate:[datePicker date]];
datePicker.hidden = NO;
[nameField resignFirstResponder];
return NO;
}
return YES;
}
- (void)dealloc
{
self.backgroundColor = nil;
self.nameField = nil;
self.dateField = nil;
self.datePicker = nil;
self.eventName = nil;
self.eventDate = nil;
[dateFormatter release];
[super dealloc];
}
#end
My use of the recorded date is similar to the following:
NSTimeInterval age = [enteredDate timeIntervalSinceNow];
//do some math on the age variable to get number of days since the event occurred.
Two options:
Option 1:
int fromDate = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSEraCalendarUnit
forDate:enteredDate];
int toDate = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSEraCalendarUnit
forDate:[NSDate date]];
int days = toDate - fromDate;
option 2:
NSDate *dateA, *dateB;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
startDate:&dateA
interval:NULL
forDate:enteredDate];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
startDate:&dateB
interval:NULL
forDate:[NSDate date]];
//From here, apply the above method using components:fromDate:toDate:
//with dateA and dateB, which are set to the beginning of the days on
//which they lie.
NSInteger days = [[[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:enteredDate
toDate:[NSDate date]
options:0] day];