本文需要实现的日期选择器和自定义选择器效果如下:

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

iOS8底部弹出日期选择或自定义选择器的方法相关推荐

  1. Android仿IOS封装通用的弹出框Dialog和底部弹出列表选择框 仿美团顶部条件筛选框 附自定义ViewGroup

    弹出框 背景 提示与询问弹出框 实现 使用 列表选择框 实现 使用 顶部条件筛选框 实现 自定义ViewGroup 使用 总结 背景 鉴于Android提供的默认弹出框很一般,IOS的弹出框样式还不错 ...

  2. Android自定义底部弹出窗-dialog(2种实现分析+源码)

    Android自定义底部弹出窗-dialog(2种实现分析+源码) 上线项目功能抽取,在项目开发中,我们会在许多地方会用到底部自定义弹窗,比如设置:个人账户退出,切换,照片的拍照或者相册的调出,或者一 ...

  3. php 点击文本框弹出时间,点击Input框弹出日期选项

    点击text文本框弹出日期选择器 body{margin:0;padding:0;font:14px Verdana, Arial, sans-serif;line-height:200%;} #co ...

  4. Android底部弹出选择框PickerView的使用

    希望大家可以留个关注点赞,后续会有更多的技术分享 前言 本次主要介绍Android中底部弹出框的使用,使用两个案例来说明,首先是时间选择器,然后是自定义底部弹出框的选择器,以下来一一说明他们的使用方法 ...

  5. 【Android】Activity和PopupWindow都实现从底部弹出或滑出选择菜单或窗口

    使用Activity和PopupWindow都实现从底部弹出或滑出选择菜单或窗口 [Android]测试与popwindow PopupWindow转载自:Android PopupWindow实现从 ...

  6. html自定义js程序,JS中微信小程序自定义底部弹出框

    实现微信小程序底部弹出框效果,代码分为html,css和js两部分,具体代码详情大家参考下本文. html CSS .commodity_screen { width: 100%; height: 1 ...

  7. android的底部弹出框炫酷的样式,Android自定义底部弹出框ButtomDialog

    本文实例为大家分享了Android自定义底部弹出框的具体代码,供大家参考,具体内容如下 先看看效果和你要的是否一样 一 .先来配置自定义控件需要的资源 1.在res文件夹下创建一个anim文件夹并创建 ...

  8. android PopupWindow实现从底部弹出或滑出选择菜单或窗口

    android PopupWindow实现从底部弹出或滑出选择菜单或窗口 http://www.open-open.com/lib/view/open1379383271818.html http:/ ...

  9. 微信小程序 -- 自定义底部弹出框(带动画--滑入滑出)

    实现这么一个功能,点击选项进行选择,效果是从底部弹出选项框(带滑出动画),选择了某项或者点击其他地方,隐藏(带滑出动画).效果图如下: 可适用于任何场景,如普通选项(如图)或者类似商城小程序选择商品属 ...

最新文章

  1. Python 标准库之 json
  2. python人工智能-Python 人工智能应用快速入门 (一)
  3. 【Python】条件及循环语句
  4. Database 2 Day DBA guide_Chapter3
  5. 【已解决】百度云分享失败
  6. 在Linux系统里安装Virtual Box的详细步骤 1
  7. post和php input,PHP“php:/Input”vs$POST
  8. 在软件开发中应用80:20原则
  9. httprequest存储的是字符内容 而文本内容是以字节形式上传的;所以普通的取值方式无法从httprequest取到值...
  10. 【源码】迭代法求根的matlab算法
  11. APICloud开源O2O商城源码
  12. 时间的几种格式以及它们之间的相互转换 (js)
  13. Juniper SRX240 U盘升级junos
  14. Django 3.1中的WebSockets
  15. tushare 获取复权数据
  16. python程序题斐波那契数列通项公式,Python斐波那契数列应用,编程练习题实例六...
  17. linux环境怎么更新离线rpm包,SUSE Linux 11系统rpm包离线安装GCC
  18. python文件输出exe文件反汇编_python 反编译exe文件为py文件的实例代码
  19. 使用VPB生成OSG的.ive格式地形方法
  20. VMware虚拟机的安装、创建及CentOS 7的安装

热门文章

  1. 【视觉SLAM14讲】ch3课后题答案
  2. 【Luogu3041】视频游戏的连击(AC自动机,动态规划)
  3. 【Java面试题】54 去掉一个Vector集合中重复的元素
  4. 素数环问题---深度搜索遍历
  5. 文件操作(ifstream、ofstream、fstream)
  6. 成都Uber优步司机奖励政策(1月9日)
  7. adodb.RecordSet的属性和方法
  8. Redis6安装配置集群cluster以及集群宕机注意事项
  9. C#实现汉字转化为拼音
  10. ASP.NET2.0中的ClientScriptManager 类用法—如何添加客户端事件!