iOS8底部彈出日期選擇或自訂選取器的方法

來源:互聯網
上載者:User

本文需要實現的日期選取器和自訂選取器效果如下:





在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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.