首先看一个时间选择器
本时间选择器是建立在弹出视图上的,也可以在普通视图上显示。
定义一个弹出框(具体的弹出框功能可以在我的另一篇文章可见):
//本方法是一个按钮的点击事件
-(void)customTime{
UIAlertController *alert;
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:^{
}];//通过模态视图模式显示UI