我有一個應用程序,我需要顯示一個彈出選擇一個日期,並將該日期寫回到視圖控制器。如何在Swift中將日期選擇器添加到UIAlertController(UIAlertView)?
我在網上搜索了很多,但沒有找到相關解決方案。
任何形式的幫助非常感謝。
我有一個應用程序,我需要顯示一個彈出選擇一個日期,並將該日期寫回到視圖控制器。如何在Swift中將日期選擇器添加到UIAlertController(UIAlertView)?
我在網上搜索了很多,但沒有找到相關解決方案。
任何形式的幫助非常感謝。
請勿使用任何形式的UIAlertView或UIAlertController。讓你的擁有自己的視圖並彈出(大概使用一個呈現的視圖控制器)。視圖可以很小,就像日期選擇器一樣,您可以在後面放置陰影視圖,就像警報一樣。
這個例子會讓你開始:https://github.com/mattneub/custom-alert-view-iOS7它不涉及日期選擇器,但它顯示瞭如何將_any_視圖變成外觀和行爲類似於警報。 – matt 2014-11-05 22:55:34
這是我的代碼:
- (id)initWithDatePicker:(NSString*)title parentView:(UIView*)parentView {
self = [super init];
if (self) {
datePickerView = [[UIDatePicker alloc] init];
datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
if (IS_IOS8_AND_UP) {
alertViewController = [UIAlertController
alertControllerWithTitle:EMPTY_STRING
message:title
preferredStyle:UIAlertControllerStyleActionSheet];
UIView* aboveBlurLayer = alertViewController.view.subviews[0];
[aboveBlurLayer addSubview:datePickerView];
[aboveBlurLayer setWidth:SCREEN_WIDTH - 16];
[datePickerView setWidth:SCREEN_WIDTH - 16];
[alertViewController.view setWidth:SCREEN_WIDTH - 16];
[alertViewController.view setBackgroundColor:[UIColor whiteColor]];
UIAlertAction* alertAction =
[UIAlertAction actionWithTitle:EMPTY_STRING
style:UIAlertActionStyleDefault
handler:nil];
[alertViewController addAction:alertAction];
[alertViewController addAction:alertAction];
[alertViewController addAction:alertAction];
[alertViewController addAction:alertAction];
[datePickerView setBackgroundColor:[UIColor whiteColor]];
[aboveBlurLayer addSubview:datePickerView];
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet addSubview:datePickerView];
}
[self addToolBar];
isDatePicker = YES;
parent = parentView;
}
return self;
}
在工具欄我已經做了兩個按鈕和取消 上完成我通過與選定的日期代表發回一個電話,就取消我解僱。 此代碼適用於iOS 7和iOS 8
'UIAlertView'已在iOS 8 SDK中棄用。你的意思是'UIAlertController'嗎?如果是這樣,這個問題可能會有所幫助:http://stackoverflow.com/questions/25545982/is-there-any-way-to-add-uipickerview-into-uialertcontroller-alert-or-actionshee – JAL 2014-11-05 21:45:32
@JAL是的,我的意思是UIAlertController 。你給出的鏈接不是關於Swift(我的意思是用戶給出的問題的答案)。我不知道Objective-C,我從Swift開始,因此我很難將這些代碼轉換成Swift。 – 2014-11-05 21:54:07