iOS8底部弹出日期选择或自定义选择器的方法
来源:互联网 发布:知金教育官网 编辑:程序博客网 时间:2024/05/16 11:54
本文需要实现的日期选择器和自定义选择器效果如下:
在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底部弹出日期选择或自定义选择器的方法
- android日期选择器,从底部弹出的日期选择器,有点像IOS日期选择器
- 自定义居于底部的选择国家的滚动选择器
- 底部弹出的选择框
- 自定义的可以只选择年月的日期选择器
- Android AndBase框架之底部弹出日期选择器
- 从底部弹出或滑出选择菜单或窗口
- android style 从底部弹出或滑出选择菜单窗口的样式
- 使用UIAlertController在底部弹出日期选择框
- AndroidIOS风格底部选择器(支持时间,日期,自定义)
- iOS 自定义微信分享底部弹出选择界面
- mui日期选择器弹出两次的解决方法
- 对话框或弹出自定义样式的3种方法
- iOS开发底部弹出年月日选择器的使用代码
- 底部弹出自定义Dialog
- 底部弹出自定义dialog
- Android好看的自定义滚动式日期选择控件和数字选择器
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- 数据库导出和导入
- erb文件与rhtml文件的区别
- Axis: rt.jar和jaxrpc中javax.xml.namespace.QName冲突问题分析
- webstorm 集成mocha 单元测试
- 层次查询测试
- iOS8底部弹出日期选择或自定义选择器的方法
- go lang学习笔记——channel机理及调度理解
- hdu2243 ac自动机+矩阵连乘
- Vijos P1323 化工厂装箱员(SHOI2001)题解
- 短小精悍:LinuxMint/Ubuntu实用小软件
- 检测weblogic的server内存溢出并将其重启的脚本
- Linux 内核 Makefile 体系简单分析
- rails中多对多表关联时处理方法总结
- 操作系统(九)——文件管理