【IOS 开发学习总结-OC-48】★★ios开发之UI控件——UIDatePicker与 UIPickerView

本文详细介绍了iOS开发中两种重要的UI控件:UIDatePicker和UIPickerView。针对UIDatePicker,讲解了其作为日期选择器和倒计时器的不同模式及其属性设置。对于UIPickerView,探讨了其单列和多列选择器的实现,包括常用属性、方法以及如何自定义外观和实现数据源与代理方法。同时,还展示了如何创建相互关联的多列选择器以及自定义选择器视图的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【IOS 开发学习总结-OC-48】★★ios开发之UI控件——UIDatePicker与 UIPickerView

UIDatePicker控件——日期选择器

用来选择日期与时间,也可以作为倒计时控件。UIDatePicker继承了 UIControl——可作为活动控件使用,与用户进行交互,既可以在 interface builder,也可以在代码中绑定事件处理方法。

这里写图片描述
属性面板简要说明:
1. mode: 4 个模式可选:①date:仅显示日期,不显示时间;②time: 仅显示时间,不显示日期;③date and time: 日期时间都显示;④count down timer: 仅显示为倒计时器。
2. locale:——设置国际化locale(场所,现场)。实际上,无需手动设置,默认使用 IOS 系统的国际化 locale;
3. interval:—— 对mode 中的后3种模式有效,设置2个时间之间的间隔;
4. constraints:——设置最小,最大时间,范围内可以选择;
5. timer:count down timer模式时有效,设置控件作为倒计时控件时剩下的秒数。

示例:

这里写图片描述
关键代码段:

- (IBAction)clicked:(id)sender {
    // 获取用户通过UIDatePicker设置的日期和时间
    NSDate *selected = [self.datePicker date];
    // 创建一个日期格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // 为日期格式器设置格式字符串
    [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm +0800"];
    // 使用日期格式器格式化日期、时间
    NSString *destDateString = [dateFormatter stringFromDate:selected];
    NSString *message =  [NSString stringWithFormat:
        @"您选择的日期和时间是:%@", destDateString];
    // 创建一个UIAlertView对象(警告框),并通过该警告框显示用户选择的日期、时间
    UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"日期和时间"
        message:message
        delegate:nil
        cancelButtonTitle:@"确定"
        otherButtonTitles:nil];
    // 显示UIAlertView
    [alert show];
}
@end

倒计时器效果

这里写图片描述
关键源码(每隔一分钟跳动一次):

#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
NSTimer* timer;
NSInteger leftSeconds;  // ①保存倒时器的剩余时间
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置使用Count Down Timer模式
    self.countDown.datePickerMode = UIDatePickerModeCountDownTimer;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)clicked:(id)sender {
    // 获取该倒计时器的剩余时间
    leftSeconds = self.countDown.countDownDuration
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值