如何在iOS7中将UIDatePicker添加到UIALertView [英] How to Add a UIDatePicker to UIALertView in iOS7
问题描述
我已经在我的一个视图中实现了datepicker,
方法是将datepicker添加到Alertview。现在在iOS 7中,我无法在alertview上找到datepicker,当我搜索到的时候,我们无法在iOS7中添加任何警报视图。
我的要求是当我选择DOB文本字段时,我必须显示Datepickerview并修改日期,更改的值将被存储到文本字段中。 >
现在我的问题是如何在alertview上显示datepicker。
我已经尝试在视图中显示,但是
我知道这是一个较老的问题,但我相信以下允许OP请求,我不认为会违反涉及AlertView层次结构更改的Apple规则:
- (void)requestDateOfBirth
{
UIDatePicker * birthDatePicker;
UIAlertView * setBirthDate;
NSDateFormatter * birthDateFormatter;
UITextField * birthDateInput;
//创建alertview
setBirthDate = [[UIAlertView alloc] initWithTitle:@出生日期
消息:nil
委托:self
cancelButtonTitle:@ 取消
otherButtonTitles:@OK,nil];
setBirthDate.alertViewStyle = UIAlertViewStylePlainTextInput;
//创建datepicker
birthDatePicker = [[UIDatePicker alloc] init];
[birthDatePicker addTarget:self action:@selector(dateChanged :) forControlEvents:UIControlEventValueChanged];
birthDatePicker.datePickerMode = UIDatePickerModeDate;
birthDatePicker.date = [NSDate date];
//获取明文中提供的textfield alertview
birthDateInput = [setBirthDate textFieldAtIndex:0];
//将textfields inputView更改为日期选择器
birthDateInput.inputView = birthDatePicker;
[birthDateInput setTextAlignment:NSTextAlignmentCenter];
//创建日期格式化程序以适当显示日期
birthDateFormatter = [[NSDateFormatter alloc] init];
[birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
//将当前日期设置为textfield
birthDateInput.text = [birthDateFormatter stringFromDate:[NSDate date]];
//显示警报视图并激活文本框
[setBirthDate show];
[birthDateInput becomeFirstResponder];
}
然后不要忘记处理日期选择器中的更改
- (void)dateChanged:(id)sender
{
NSDateFormatter * birthDateFormatter;
UIDatePicker * birthDatePicker =(UIDatePicker *)发件人;
birthDateFormatter = [[NSDateFormatter alloc] init];
[birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
birthDateInput.text = [birthDateFormatter stringFromDate:birthDatePicker.date];
}
我希望这有助于
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.
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
这篇关于如何在iOS7中将UIDatePicker添加到UIALertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!