国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS中使用UIDatePicker制作時(shí)間選擇器的實(shí)例教程

2020-07-26 03:21:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

UIDatePicker的創(chuàng)建
UIDatePicker是一個(gè)可以用來(lái)選擇或者設(shè)置日期的控件,不過(guò)它是像轉(zhuǎn)輪一樣的控件,而且是蘋果專門為日歷做好的控件,如下圖所示:

20165591815521.png (320×215)

除了UIDatePicker控件,還有一種更通用的轉(zhuǎn)輪形的控件:UIPickerView,只不過(guò)UIDatePicker控件顯示的就是日 歷,而UIPickerView控件中顯示的內(nèi)容需要我們自己用代碼設(shè)置。本篇文章簡(jiǎn)單介紹UIDatePicker控件,后邊的文章會(huì)介紹 UIPickerView。

1、運(yùn)行Xcode ,新建一個(gè)Single View Application,名稱為UIDatePicker Test,其他設(shè)置如下圖所示:

20165591843116.png (531×238)

2、單擊ViewController.xib,打開(kāi)Interface Builder。拖一個(gè)UIDatePicker控件到視圖上:

20165591859300.jpg (1087×596)

3、然后拖一個(gè)按鈕在視圖上,并修改按鈕名稱為Select:

20165591922016.png (414×151)

單擊按鈕后,彈出一個(gè)Alert,用于顯示用戶所作選擇。

4、創(chuàng)建映射:打開(kāi)Assistant Editor,選中UIDatePicker控件,按住Control,拖到ViewController.h中:

20165591940085.png (830×325)

新建一個(gè)Outlet,名稱為datePicker:

20165591957371.png (582×184)

然后以同樣的方式為按鈕建立一個(gè)Action映射,名稱為buttonPressed,事件類型為默認(rèn)的Touch Up Inside。

5、選中UIDatePicker控件,打開(kāi)Attribute Inspector,在其中設(shè)置Maximum Date比如我們這里設(shè)為2100-12-31:

20165592012899.png (768×252)


實(shí)例
而今天我們要做的時(shí)間選取器成品具體效果如下:

20165592034703.png (656×421)

我們自定義一個(gè)LGDatePickerView,在LGDatePickerView里面實(shí)現(xiàn)。

背景半透明:

背景是半透明的,點(diǎn)擊的灰色背景的時(shí)候,時(shí)間選取器消失。在LGDatePickerView初始化方法里,代碼如下:

復(fù)制代碼 代碼如下:

- (id)init
{
    self = [super init];
    if (self) {
  //背景半透明,綁定取消方法
    UIControl *control = [[UIControl alloc] initWithFrame:SCREEN_BOUNDS];
    control.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5f];
    [self addSubview:control];
    [control addTarget:self action:@selector(actionCancel:) forControlEvents:UIControlEventTouchUpInside];    
     }
    return self;
}

綁定的actionCancel方法:
復(fù)制代碼 代碼如下:

- (void)actionCancel:(id)sender
{
    [self removeFromSuperview];
}

確定取消按鈕:

看到上面的確定取消按鈕,你會(huì)怎么做,寫一個(gè)UIView上面放兩個(gè)UIButton。這樣做也是可以實(shí)現(xiàn)的。我們還可以用UIToolbar。在LGDatePickerView初始化方法里加上下面這段代碼:

復(fù)制代碼 代碼如下:

 // Toolbar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, SCREEN.height - 250, SCREEN.width, 50)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *itemCancelDone = [[UIBarButtonItem alloc] initWithTitle:@"確定" style:UIBarButtonItemStylePlain target:self action:@selector(actionConfirm:)];
UIBarButtonItem *itemCancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(actionCancel:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:itemCancel,space,itemCancelDone, nil]];
[control addSubview:toolbar];

actionCancel上面已經(jīng)實(shí)現(xiàn)了。下面實(shí)現(xiàn)actionConfirm方法。它有什么作用呢?

點(diǎn)擊的時(shí)候獲取到時(shí)間,然后通過(guò)代理代理出去。
時(shí)間選取器消失:

復(fù)制代碼 代碼如下:

  - (void)actionConfirm:(id)sender
  {
      if ([self.delegate respondsToSelector:@selector(datePickerView:didSelectTime:)]) {
          [self.delegate datePickerView:self didSelectTime:self.datePicker.date];
      }
      [self removeFromSuperview];
  }

代理方法:

在LGDatePickerView.h

復(fù)制代碼 代碼如下:

@protocol LGDatePickerViewDelegate <NSObject>

- (void)datePickerView:(LGDatePickerView *)datepicker didSelectTime:(NSDate *)time;

@end


創(chuàng)建UIDatePicker:

在LGDatePickerView.h定義一個(gè)全局變量

復(fù)制代碼 代碼如下:

@property (nonatomic, strong) UIDatePicker *datePicker;

在LGDatePickerView初始化方法里加上下面這段代碼:
復(fù)制代碼 代碼如下:

UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.backgroundColor = [UIColor whiteColor];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, SCREEN.height - 200, SCREEN.width, 220);
[control addSubview:datePicker];
self.datePicker = datePicker;

使用LGDatePickerView

使用起來(lái)很簡(jiǎn)單,創(chuàng)建一下,然后加載self.view上面即可:

復(fù)制代碼 代碼如下:

    LGDatePickerView *datePicker = [[LGDatePickerView alloc] init];
    datePicker.delegate = self;
    datePicker.datePicker.date = [NSDate date];
    datePicker.frame = self.view.bounds;
    [self.view addSubview:datePicker];

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乾安县| 准格尔旗| 万州区| 林西县| 甘肃省| 贵溪市| 奉节县| 乌鲁木齐县| 彰武县| 石景山区| 大兴区| 霍州市| 仙居县| 绥滨县| 都匀市| 宜宾市| 唐河县| 梨树县| 扶绥县| 卫辉市| 芜湖市| 福海县| 梁山县| 内丘县| 静安区| 揭阳市| 正安县| 睢宁县| 博白县| 龙山县| 新兴县| 绥棱县| 洛浦县| 万山特区| 平阳县| 顺平县| 泰兴市| 丘北县| 双城市| 莱州市| 丹寨县|