IOS 7 Study - UIDatePicker

简介: Picking the Date and Time with UIDatePicker   effect:   1. declaring a property of type UIDatePicker #import "ViewController.

Picking the Date and Time with UIDatePicker

 

effect:

 

1. declaring a property of type UIDatePicker

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDatePicker *myDatePicker;

@end

@implementation ViewController

...

 

2. instantiate the date picker

- (void)viewDidLoad{
  [super viewDidLoad];

  self.myDatePicker = [[UIDatePicker alloc] init];
  self.myDatePicker.center = self.view.center;

  [self.view addSubview:self.myDatePicker];
}

 

3. add receiver target 

Just like the UISwitch class, a date picker sends action messages to its targets whenever
the user has selected a different date. To respond to these messages, the receiver must
add itself as the target of the date picker, using the addTarget:action:forControlEvents

- (void)viewDidLoad{
  [super viewDidLoad];

  self.myDatePicker = [[UIDatePicker alloc] init];
  self.myDatePicker.center = self.view.center;
  
  [self.view addSubview:self.myDatePicker];
  [self.myDatePicker addTarget:self
     action:@selector(datePickerDateChanged:)
     forControlEvents:UIControlEventValueChanged];
}

 

4. accessing the date

 

// you can attempt to retrieve its currently selected date using its date property

NSDate *currentDate = self.myDatePicker.date;
NSLog(@"Date = %@", currentDate);

 

// every time the user changes the date, get a message from the date picker

- (void) datePickerDateChanged:(UIDatePicker *)paramDatePicker{
  if ([paramDatePicker isEqual:self.myDatePicker]){
    NSLog(@"Selected date = %@", paramDatePicker.date);
  }
}

 

A date picker also lets you set the minimum and the maximum dates that it can display.
For this, let’s first switch our date picker mode to UIDatePickerModeDate and then,
using the maximumDate and the minimumDate properties, adjust this range:

 

- (void)viewDidLoad{
  [super viewDidLoad];

  self.myDatePicker = [[UIDatePicker alloc] init];
  self.myDatePicker.center = self.view.center;
  self.myDatePicker.datePickerMode = UIDatePickerModeDate;

  [self.view addSubview:self.myDatePicker];
  
  NSTimeInterval oneYearTime = 365 * 24 * 60 * 60;
  NSDate *todayDate = [NSDate date];
  NSDate *oneYearFromToday 
    = [todayDate dateByAddingTimeInterval:oneYearTime];
  NSDate *twoYearsFromToday 
    = [todayDate dateByAddingTimeInterval:2 * oneYearTime];
  self.myDatePicker.minimumDate = oneYearFromToday;
  self.myDatePicker.maximumDate = twoYearsFromToday;
}

 

If you want to use the date picker as a countdown timer, you must set your date picker
mode to UIDatePickerModeCountDownTimer and use the countDownDuration property
of the date picker to specify the default countdown duration.

 

- (void)viewDidLoad{
  [super viewDidLoad];

  self.myDatePicker = [[UIDatePicker alloc] init];
  self.myDatePicker.center = self.view.center;
  self.myDatePicker.datePickerMode = UIDatePickerModeCountDownTimer;
  
  [self.view addSubview:self.myDatePicker];

  NSTimeInterval twoMinutes = 2 * 60;
  [self.myDatePicker setCountDownDuration:twoMinutes];
}

 

Run Result:

 

 

 

 

 

 

 

 

目录
相关文章
|
iOS开发
iOS开发UI之日期控件的使用(UIDatePicker)
iOS开发UI之日期控件的使用(UIDatePicker)
441 0
|
iOS开发 Swift
iOS开发技巧 - 使用UIDatePicker来选择日期和时间
(Swift) import UIKit class ViewController: UIViewController { var datePicker: UIDatePicker! func datePickerDateChanged(datePicke...
1777 0
|
iOS开发 Swift
iOS - UIDatePicker
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIDatePicker : UIControl @available(iOS 2.0, *) public class UIDatePicker : UIControl, NSCoding UIDatePicker 是 UIKit 控件中提供日期和时间选择的控件。
1045 0
|
5天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
1月前
|
Java Android开发 Swift
安卓与iOS开发对比:平台选择对项目成功的影响
【10月更文挑战第4天】在移动应用开发的世界中,选择合适的平台是至关重要的。本文将深入探讨安卓和iOS两大主流平台的开发环境、用户基础、市场份额和开发成本等方面的差异,并分析这些差异如何影响项目的最终成果。通过比较这两个平台的优势与挑战,开发者可以更好地决定哪个平台更适合他们的项目需求。
112 1
|
12天前
|
安全 数据处理 Swift
深入探索iOS开发中的Swift语言特性
本文旨在为开发者提供对Swift语言在iOS平台开发的深度理解,涵盖从基础语法到高级特性的全面分析。通过具体案例和代码示例,揭示Swift如何简化编程过程、提高代码效率,并促进iOS应用的创新。文章不仅适合初学者作为入门指南,也适合有经验的开发者深化对Swift语言的认识。
34 9
|
9天前
|
设计模式 Swift iOS开发
探索iOS开发:从基础到高级,打造你的第一款App
【10月更文挑战第40天】在这个数字时代,掌握移动应用开发已成为许多技术爱好者的梦想。本文将带你走进iOS开发的世界,从最基础的概念出发,逐步深入到高级功能实现,最终指导你完成自己的第一款App。无论你是编程新手还是有志于扩展技能的开发者,这篇文章都将为你提供一条清晰的学习路径。让我们一起开始这段旅程吧!
|
12天前
|
Android开发 Swift iOS开发
探索安卓与iOS开发的差异和挑战
【10月更文挑战第37天】在移动应用开发的广阔舞台上,安卓和iOS这两大操作系统扮演着主角。它们各自拥有独特的特性、优势以及面临的开发挑战。本文将深入探讨这两个平台在开发过程中的主要差异,从编程语言到用户界面设计,再到市场分布的不同影响,旨在为开发者提供一个全面的视角,帮助他们更好地理解并应对在不同平台上进行应用开发时可能遇到的难题和机遇。
|
10天前
|
iOS开发 开发者
探索iOS开发中的SwiftUI框架
【10月更文挑战第39天】在苹果的生态系统中,SwiftUI框架以其声明式语法和易用性成为开发者的新宠。本文将深入SwiftUI的核心概念,通过实际案例展示如何利用这一框架快速构建用户界面,并探讨其对iOS应用开发流程的影响。