转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982
一. 日期选择器 (UIDatePicker)UIDatePicker 属性截图 :
Mode 属性 : 用于设置 UIDatePicker 模式;
-- Date 属性值 : 显示日期, 不显示时间;
-- Time 属性值 : 显示时间, 不显示日期;
-- Date and Time 属性值 : 同时显示日期 和 时间;
-- Count Down Timer 属性值 : 显示倒计时器;
(2) Local 属性Local 属性值 : 国际化设置相关属性, 通常使用默认的设置即可;
(3) Interval 属性Interval 属性值 : 设置两个时间选项的时间间隔, 仅当采用 Time, Date and Time, Count Down Timer 三个属性时有效;
(4) Constraints 属性Constraints 属性 : 设置控件的最小时间 和 最大时间;
(5) Timer 属性Timer 属性 : 设置倒计时 的计时秒数, 只有采用 Count Down Timer 模式时有效;
2. UIDatePicker 日期选择器 示例代码代码示例 :
-- 界面设计文件 :
-- OCViewController.h :
//
// OCViewController.h
// UIDatePicker
//
// Created by octopus on 15-12-19.
// Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
//
#import
@interface OCViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
- (IBAction)click:(id)sender;
@end
-- OCViewController.m :
//
// OCViewController.m
// UIDatePicker
//
// Created by octopus on 15-12-19.
// Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
//
#import "OCViewController.h"
@interface OCViewController ()
@end
@implementation OCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender {
//从 UIdatePicker 中获取事件
NSDate * date = [self.datePicker date];
//创建 Date 格式化工具
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
//格式日期格式化格式
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//将日期转为格式化好的字符串
NSString * dateString = [formatter stringFromDate:date];
//创建 警告对话框
NSString * message = [NSString stringWithFormat:@"UIDatePicker 被选中的时间是 %@", dateString];
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"时间选择" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//显示警告对话框
[alertView show];
}
@end
-- 界面运行效果 :
计时器示例代码 :
-- 界面设计文件 :
-- OCViewController.h :
//
// OCViewController.h
// UIDatePicker
//
// Created by octopus on 15-12-19.
// Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
//
#import
@interface OCViewController : UIViewController
//日期选择器
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
//计时器
@property (strong, nonatomic) IBOutlet UIDatePicker *countDown;
//计时器按钮控件
@property (strong, nonatomic) IBOutlet UIButton *countDownBt;
//日期选择器下的按钮方法
- (IBAction)click:(id)sender;
//计时器下的按钮方法
- (IBAction)clickCountDown:(id)sender;
@end
-- OCViewController.m :
//
// OCViewController.m
// UIDatePicker
//
// Created by octopus on 15-12-19.
// Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
//
#import "OCViewController.h"
@interface OCViewController ()
@end
@implementation OCViewController
NSTimer * timer;
NSInteger second;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设置 UIDatePicker 模式为 计时器模式
self.countDown.datePickerMode = UIDatePickerModeCountDownTimer;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender {
//从 UIdatePicker 中获取事件
NSDate * date = [self.datePicker date];
//创建 Date 格式化工具
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
//格式日期格式化格式
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//将日期转为格式化好的字符串
NSString * dateString = [formatter stringFromDate:date];
//创建 警告对话框
NSString * message = [NSString stringWithFormat:@"UIDatePicker 被选中的时间是 %@", dateString];
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"时间选择" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//显示警告对话框
[alertView show];
}
- (IBAction)clickCountDown:(id)sender {
NSLog(@"点击了 clickCountDown");
//获取倒计时剩余时间
second = self.countDown.countDownDuration;
//设置 UIDatePicker 和 倒计时按钮 禁用状态
self.countDown.enabled = NO;
[sender setEnabled:NO];
//生成当前秒数 字符串
NSString * message = [NSString stringWithFormat:@"倒计时 剩余 %d", second];
//创建警告框
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"倒计时" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//显示警告框
[alert show];
//启动定时器, 每隔 1 秒调用一次
timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(tick) userInfo:nil repeats:YES];
}
-(void) tick{
//秒数 减去 60
second = second - 60;
//将自减后的秒数设置给 UIDatePicker
self.countDown.countDownDuration = second;
//如果倒计时完毕, 控件 恢复正常
if(second = 1.0) {
[timer invalidate];
}else{
[progress setProgress:progress.progress + 0.01 animated:YES];
}
}
@end
-- 运行效果 :
2015-12-29 15:17:55.644 UItoolBar[2331:60b] 点击
2015-12-29 15:17:58.925 UItoolBar[2331:60b] 点击
2015-12-29 15:18:00.669 UItoolBar[2331:60b] 点击
2015-12-29 15:18:07.831 UItoolBar[2331:60b] tag : 1, tittle : Left
2015-12-29 15:18:09.446 UItoolBar[2331:60b] tag : 2, tittle : Center
2015-12-29 15:18:10.961 UItoolBar[2331:60b] tag : 3, tittle : Right
转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982