UIDatePicker

一, 效果图。

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController {
    UIDatePicker *datePicker;
    UILabel *timeLabel;
}

@end
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title=@"timerPicker";

    timeLabel=[[UILabel alloc]init];
    timeLabel.backgroundColor=[UIColor redColor];
    timeLabel.frame=CGRectMake(10, 100, 300, 50);
    timeLabel.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:timeLabel];

}
//点击任意处,弹出时间选择框
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-216, 0, 0)];
    datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
    [self.view addSubview:datePicker];
}

//开始结束时间选择框
- (void)dateChanged:(id)sender
{
    NSDate *date = datePicker.date;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    //设备默认的值
    dateFormatter.locale=[NSLocale systemLocale];
    //将所选择的时候转化为需要的时间格式
    NSString *dateStr = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
    timeLabel.text=dateStr;

    //让选择完时间后,选择框,消失,这个时间可以自己调整
    [UIView animateWithDuration:.5f delay:.5f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [datePicker setFrame:CGRectMake(0, self.view.frame.size.height, 0, 0)];
    } completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
时间: 2024-08-09 02:19:44

UIDatePicker的相关文章

Objective - C 学习笔记:UIPickerView 和 UIDatePicker的基本使用

1.UIPickerView 1.1. UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource; // 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择) @property(nonatomic,assign) id<UIPickerViewDelegate>

UIPickView 和 UIDatePicker

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

UIDatePicker的使用

UIDatePicker控件特别类似与UIPickerView控件.主要用来对日期的选择. 1. UIDatePicker的实例化和基本设置 self.myDatePicker = [[UIDatePicker alloc] init]; [self.myDatePicker setCenter:self.view.center]; // 通过setDatePickerMode方法,来设置UIDatePicker的样式. [self.myDatePicker setDatePickerMode:

UI进阶之UIDatePicker键盘处理

一:常用属性 @property (nonatomic) UIDatePickerMode datePickerMode; // 日期选择模式,默认UIDatePickerModeDateAndTime @property (nonatomic, retain) NSLocale   *locale;   // 语言环境,默认[NSLocale currentLocale],nil使用默认值 @property (nonatomic, retain) NSDate *date;// 当前时间 二

简述UIDatePicker的用法

1.Locale 设置DatePicker的地区,即设置DatePicker显示的语言. 1.跟踪所有可用的地区,取出想要的地区 NSLog(@"%@", [NSLocale availableLocaleIdentifiers]); 2. 设置日期选择控件的地区 [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]]; 效果: [datePicker setLo

iOS开发——高级UI之OC篇&amp;UIdatePicker&amp;UIPickerView简单使用

UIdatePicker&UIPickerView简单使用 /***********************************************************************************/ 一:UIdatePicker:(日期控件) 1.UIDatePicker什么时候用? 当用户选择日期的时候,一般弹出一个UIDatePicker给用户选择. 2.UIDatePickerios6和ios7/8的区别 下面看看使用封装的代码怎么去实现它: 因为这个比较简

iOS.UIKit.10.UIDatePicker

一.案例介绍:点击屏幕中的UIButton,将在UIDatePicker选择的日期显示在UILabel中,如图01,02 图01图02 二.案例步骤: 1.选择Simple View Aplication,取名cq.36.日期选择器,如图03 图03 2.Main.storyboard,如图04 3.CQ36ViewController.h #import <UIKit/UIKit.h> @interface CQ36ViewController : UIViewController @pro

UIDatePicker的简单用法

// 初始化UIDatePickerUIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];// 设置时区[datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];// 设置当前显示时间[datePicker setDate:tempDate animated:YES];// 设置显示最大时间

Objective-c 手写UIDatePicker

// //  ViewController.m //  手写UIDatePicker // //  Created by 非凡 程序员 on 15/11/13. //  Copyright (c) 2015年 非凡 程序员. All rights reserved. // .h文件 #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(nonatomic,strong)UIDatePicke