本文需要實現的日期選取器和自訂選取器效果如下:
在iOS8之前,可以通過UIActionSheet來實現,在iOS8之後,可以通過UIAlertController實現,UIAlertController的官方解釋如下:
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method
In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction: method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object.
官方文檔主要講的是AlertController取代了UIActionSheet和UIAlertView,並且不再使用show的方式調用顯示,而是通過模態視圖的方式。另外通過addAction的方法來添加響應按鈕,並通過block來處理點擊結果。
彈出日期選取器的代碼如下:
<pre name="code" class="objc">-(void)customTime{ if (!alert) { alert = [UIAlertController alertControllerWithTitle:@"選擇時間" message:@"\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];//初始化一個標題為“選擇時間”,風格是ActionSheet的UIAlertController,其中"\n"是為了給DatePicker騰出空間 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { //點擊確定按鈕的事件處理 }]; UIDatePicker *datePicker = [[UIDatePicker alloc] init];//初始化一個UIDatePicker [alert.view addSubview:datePicker];//將datePicker添加到UIAlertController執行個體中 [alert addAction:cancel];//將確定按鈕添加到UIAlertController執行個體中 } [self presentViewController:alert animated:YES completion:^{ }];//通過模態視圖模式顯示UIAlertController,相當於UIACtionSheet的show方法}
彈出自訂選取器的代碼如下:
-(void)customTime{ if (!alert) { alert = [UIAlertController alertControllerWithTitle:@"選擇時間" message:@"\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];//初始化一個標題為“選擇時間”,風格是ActionSheet的UIAlertController,其中"\n"是為了給DatePicker騰出空間 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { //點擊確定按鈕的事件處理 }]; //初始化選取器,並設定資料來源和代理 for (int i=1; i<=60; i++) { [timeArr addObject:[[NSString alloc] initWithFormat:@"%d",i]]; } timePicker = [[UIPickerView alloc] init]; timePicker.delegate = self; timePicker.dataSource = self; [timePicker selectRow:29 inComponent:0 animated:NO]; [alert.view addSubview:timePicker]; [alert addAction:cancel]; } [self presentViewController:alert animated:YES completion:^{ }];/通過模態視圖模式顯示UIAlertController,相當於UIACtionSheet的show方法}
其中timePicker是UIPickerView的執行個體,需要實現如下兩個代理的方法,UIPickerViewDelegate,UIPickerViewDataSource,相關代碼:
#pragma mark - UIPicker Delegate-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return [timeArr count];}-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)]; timeLabel.text = [[NSString alloc] initWithFormat:@"%@ 分鐘",[timeArr objectAtIndex:row]]; timeLabel.textAlignment = NSTextAlignmentCenter; return timeLabel;}