How to Add a UIDatePicker to UIALertView in iOS7

一世执手 提交于 2019-12-06 03:07:50

I know this is an older question, but I believe the following allows what the OP requested and I don't think will violate Apple's rules involving AlertView hierarchy changes:

- (void)requestDateOfBirth
{
UIDatePicker *birthDatePicker;
UIAlertView *setBirthDate;
NSDateFormatter *birthDateFormatter;
UITextField *birthDateInput;
//create the alertview
setBirthDate = [[UIAlertView alloc] initWithTitle:@"Date of Birth:"
                                              message:nil
                                             delegate:self
                                    cancelButtonTitle:@"Cancel"
                                    otherButtonTitles:@"OK", nil];
setBirthDate.alertViewStyle = UIAlertViewStylePlainTextInput;

//create the datepicker
birthDatePicker = [[UIDatePicker alloc] init];
[birthDatePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
birthDatePicker.datePickerMode = UIDatePickerModeDate;    
birthDatePicker.date = [NSDate date];
//get the textfield provided in the plain text alertview
birthDateInput = [setBirthDate textFieldAtIndex:0];
//change the textfields inputView to the date picker
birthDateInput.inputView = birthDatePicker;
[birthDateInput setTextAlignment:NSTextAlignmentCenter];
//create a date formatter to suitably show the date
birthDateFormatter = [[NSDateFormatter alloc] init];
[birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
//set the current date into the textfield
birthDateInput.text = [birthDateFormatter stringFromDate:[NSDate date]];
//show the alert view and activate the textfield
[setBirthDate show];
[birthDateInput becomeFirstResponder];
}

Then don't forget the handle the changes in the date picker

- (void) dateChanged:(id)sender
{
NSDateFormatter *birthDateFormatter;
UIDatePicker *birthDatePicker = (UIDatePicker *)sender;

birthDateFormatter = [[NSDateFormatter alloc] init];
[birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
birthDateInput.text = [birthDateFormatter stringFromDate:birthDatePicker.date];
}

I hope this helps

malex

In iOS 7 you can use

[alertView setValue:yourDataPicker forKey:@"accessoryView"];

Full explanation is here

The hierarchy of UIAlertView is private and should not be modified.

From Apple docs

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Until iOS 6, you could add some views to the UIAlertView subviews hierarchy, but this doesn’t seems to work anymore on iOS 7. The contentView property that was announced for iOS7 was made a private ivar since beta4 release, thus making it impossible to addSubview to alert view.

UIView *_contentViewNeue;

For more on this refer this Apple dev forum discussion (You will need to login)

In such situations, you should create a custom view mimicking the UIAlertView and then use that to add the picker view.

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!