3

I have implemented datepicker in one of my view, The way is added the datepicker to the Alertview. Now in iOS 7 i am not able to find the datepicker on the alertview, When searched i have got, we cannot add anything to alertview in iOS7.

My requirement was When i will select the DOB textfield, I have to show the Datepickerview and modify the date and the changed value will be stored into the textfield.

Now my problem is How can i show the datepicker on the alertview.

I have tried showing on the view, but when trying to remove the datpicker from view it is not working.

3 Answers 3

3

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

0
1

In iOS 7 you can use

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

Full explanation is here

0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.