// In .h file
UIDatePicker *datePicker;
[self showDatePicker: UIDatePickerModeDate];
or
or
[self showDatePicker: UIDatePickerModeTime];
// Method to show DatePicker on ActionSheet
-(void) showDatePicker: (UIDatePickerMode) modeDatePicker
{
UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
[viewDatePicker setBackgroundColor:[UIColor clearColor]];
//Make a frame for the picker & then create the picker
CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePicker.datePickerMode = modeDatePicker;
datePicker.hidden = NO;
[viewDatePicker addSubview:datePicker];
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:@"\n\n\n\n\n\n\n\n\n\n"
preferredStyle:UIAlertControllerStyleActionSheet];
[alertController.view addSubview:viewDatePicker];
UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
//Detect particular click by tag and do some thing here
if(tagDateField == 0 || tagDateField == 2)
{
[self setSelectedDateInField];
}
else if (tagDateField == 1 || tagDateField == 3)
{
[self setSelecteedTimeInField];
}
NSLog(@"OK action");
}];
[alertController addAction:doneAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
[alertController addAction:cancelAction];
/*
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Reset", @"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(@"Reset action");
}];
[alertController addAction:resetAction];
*/
[self presentViewController:alertController animated:YES completion:nil];
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Done" otherButtonTitles:nil, nil];
[actionSheet addSubview:viewDatePicker];
[actionSheet showInView:self.view];
}
}
// Methods to set selected date and time in related field
-(void)setSelectedDateInField
{
NSLog(@"date :: %@",datePicker.date.description);
//set Date formatter
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"MM/dd/YY"];
if(tagDateField == 0)
{
[txtExpirationDate setText:[formatter1 stringFromDate:datePicker.date]];
}
else if (tagDateField == 2)
{
[txtBeginDate setText:[formatter1 stringFromDate:datePicker.date]];
}
}
-(void)setSelecteedTimeInField
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"HH:mm"];
if (tagDateField == 3)
{
[txtBeginTime setText:[dateFormatter stringFromDate:datePicker.date]];
}
else if (tagDateField == 1)
{
[txtExpirationTime setText:[dateFormatter stringFromDate:datePicker.date]];
}
}
// Action sheet Delegate In case of iOS 7
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
if(tagDateField == 0 || tagDateField == 2)
{
[self setSelectedDateInField];
}
else if (tagDateField == 1 || tagDateField == 3)
{
[self setSelecteedTimeInField];
}
}
}
what is tagDateField? where is it defined?
ReplyDeletetagDateField is a int type variable which is defined in .h file of view controller.
ReplyDeletewhere is the definition for txtBeginTime, txtExpirationTime
ReplyDelete