Trying to show UIDatePickerView
as dialog box
like it shows in Android
. I tried to add it on UIAlertView
but not able to add.
Anyone tried this?
Trying to show UIDatePickerView
as dialog box
like it shows in Android
. I tried to add it on UIAlertView
but not able to add.
Anyone tried this?
AddSubview is not available in
UIAlertView
in iOS7
The UIAlertView
class is intended to be used as-it-is and does not support subclassing.
The view hierarchy for this class is private and must not be modified.
You should use UIAlertViewcontoller
. Here is the code :
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:picker];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"OK");
NSLog(@"%@",picker.date);
}];
action;
})];
UIPopoverPresentationController *popoverController = alertController.popoverPresentationController;
popoverController.sourceView = sender;
popoverController.sourceRect = [sender bounds];
[self presentViewController:alertController animated:YES completion:nil];
Hope this will help you....
In button action I set the datePicker
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
UIDatePicker *datepicker;
}
@end
@implementation ViewController
-(IBAction) showDatePicker:(id)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleAlert];
datepicker = [[UIDatePicker alloc]init];
[datepicker setDatePickerMode:UIDatePickerModeDateAndTime];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:@selector(getPickerValue:) forControlEvents:UIControlEventValueChanged];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The selected or picked datePicker value is - %@",datepicker.date);
txtfldGetDatePickerValue.text = [self getStringDateFromDate:datepicker.date];
}];
action;
})];
[alertController.view addSubview:datepicker];
[self presentViewController:alertController animated:YES completion:nil];
}
-(void)getPickerValue:(UIDatePicker *)pickerDate
{
txtfldGetDatePickerValue.text = [self getStringDateFromDate:pickerDate.date];
}
-(NSString *)getStringDateFromDate:(NSDate *)pickerDate
{
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
return [formatter stringFromDate:pickerDate];
}
I tried many solutions and I got the output finally.Since I did not give the answer properly I wanted to give best answer for your question.I googled so many answer.Even I created sample project to achieve this.I did not use action sheet and popoverpresentration view controller.Simply I used the coding with UIAlertViewController.
UIDatePicker to action sheet was discouraged by Apple all along
This will work.. check out the code.This will work in ios7 and above
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Date" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)];
[alert addSubview:picker];
alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20);
[alert setValue:picker forKey:@"accessoryView"];
[alert show];
For Showing Popover in Phone Just Make Category of UIPopoverController Code :
// UIPopoverController+iPhone.h
#import <UIKit/UIKit.h>
@interface UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled;
@end
// UIPopoverController+iPhone.m
#import "UIPopoverController+iPhone.h"
@implementation UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled {
return NO;
}
@end
This Will Help you Surely.