IOS开发之DatePicker控件详解

iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式。

,  

您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四种模式。


步骤:1.新建一个Singe View Application工程 ,拖动一个DatePicker控件 和 Button到界面。

            2.然后关联Button事件(可以按住Ctrl拖动实现),如下图。

   


3.实现代码


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    /*typedef enum {
        UIDatePickerModeTime,
        UIDatePickerModeDate,
        UIDatePickerModeDateAndTime,
        UIDatePickerModeCountDownTimer
    } UIDatePickerMode;
     */
    //datePicker.datePickerMode = UIDatePickerModeDateAndTime;//模式
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)datePick:(id)sender {
    NSDate *select  = [datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
    NSString *dateAndTime = [dateFormatter stringFromDate:select];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" 时间提示" message: dateAndTime delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    
}
 
 


效果如下



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值