3

ビューの 1 つに datepicker を実装しました。datepicker を Alertview に追加する方法です。現在、iOS 7 ではアラートビューで日付ピッカーを見つけることができません。検索すると、iOS7 のアラートビューに何も追加できません。

私の要件は、DOBテキストフィールドを選択するときに、Datepickerviewを表示して日付を変更する必要があり、変更された値がテキストフィールドに保存されることでした。

今私の問題は、アラートビューに日付ピッカーを表示するにはどうすればよいですか。

ビューに表示しようとしましたが、ビューから datpicker を削除しようとすると機能しません。

4

3 に答える 3

3

これは古い質問であることは知っていますが、以下はOPが要求したものを許可すると信じており、AlertView階層の変更に関するAppleの規則に違反するとは思いません:

- (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];
}

次に、日付ピッカーの変更を処理することを忘れないでください

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

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

これが役立つことを願っています

于 2014-02-03T13:06:15.083 に答える
1

iOS 7では、使用できます

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

完全な説明はこちら

于 2014-02-17T10:11:07.747 に答える
0

の階層UIAlertViewは非公開であり、変更しないでください。

アップルのドキュメントから

UIAlertView クラスはそのまま使用することを意図しており、サブクラス化はサポートしていません。このクラスのビュー階層は非公開であり、変更してはなりません。

iOS 6 まではサブビュー階層にいくつかのビューを追加できましたUIAlertViewが、これは iOS 7 では機能しないようです。見る。contentViewaddSubview

UIView *_contentViewNeue;

詳細については、このApple dev フォーラムのディスカッションを参照してください(ログインする必要があります)。

このような状況では、 を模倣したカスタム ビューを作成し、UIAlertViewそれを使用してピッカー ビューを追加する必要があります。

それが役立つことを願っています!

于 2013-09-24T07:32:16.827 に答える