zoukankan      html  css  js  c++  java
  • iOS学习之UIDatePicker控件使用

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

    ,  

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

    本篇文章简单介绍下PickerDate控件的使用

     

    1、新建一个Singe View Application,命名为DatePickDemo,其他设置如图

    2、放置控件

    打开ViewController.xib,拖拽一个DatePicker控件放到界面上,再拖拽一个Button控件放到界面上,双击Button,输入"选择日期时间"

    3、建立xib和ViewController的关联

    按下command+alt+enter键打开Assistant Editor,选中DatePicker按住Control键,拖拽到viewController.h上,

    建立Outlet datePicker。

    以同样方式给Button建立一个Action关联映射,命名为selectDate,事件类型为默认的Touch Up Inside。

    4、实现代码

    单击ViewController.m,找到刚才创建的

     

    - (IBAction)selectDate:(id)sender {

    }

    在这里添加响应代码

     

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

    运行看效果:

     

    5、修改模式成Date模式,修改代码

     

     [dateFormatter setDateFormat:@"yyyy-MM-dd"];  
  • 相关阅读:
    Vmware安装Ubuntu ==> 连网成功
    在 ns3.25中添加 plc(电力线载波) 模块
    Ubuntu12.04下安ns3.29及Ubuntu换源方法
    微信支付小程序版
    微信小程序打开外部链接
    linux下安装Mongodb
    延迟执行+异步 之代码分析1
    Vue入门
    实验一
    实验二
  • 原文地址:https://www.cnblogs.com/Sucri/p/4873977.html
Copyright © 2011-2022 走看看