(Swift)

import UIKit

class ViewController: UIViewController {
var datePicker: UIDatePicker! func datePickerDateChanged(datePicker: UIDatePicker) {
println("Selected date = \(datePicker.date)")
} override func viewDidLoad() {
super.viewDidLoad() datePicker = UIDatePicker()
datePicker.center = view.center
view.addSubview(datePicker) datePicker.addTarget(self,
action: "datePickerDateChanged:",
forControlEvents: .ValueChanged) /*
set the minimum and the maximum dates that it can display
*/ let oneYearTime:NSTimeInterval = * * *
let todayDate = NSDate()
let oneYearFromToday = todayDate.dateByAddingTimeInterval(oneYearTime)
let twoYearsFromToday = todayDate.dateByAddingTimeInterval( * oneYearTime)
datePicker.minimumDate = oneYearFromToday
datePicker.maximumDate = twoYearsFromToday
}
}

(Objective-C)

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDatePicker *myDatePicker;

@end

@implementation ViewController

- (void) datePickerDateChanged:(UIDatePicker *)paramDatePicker {
if ([paramDatePicker isEqual:self.myDatePicker]) {
NSLog(@"Selected date = %@", paramDatePicker.date);
}
} - (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]; NSTimeInterval oneYearTime = * * * ;
NSDate *todayDate = [NSDate date];
NSDate *oneYearFromToday = [todayDate dateByAddingTimeInterval:oneYearTime];
NSDate *twoYearsFromToday = [todayDate dateByAddingTimeInterval: * oneYearTime];
self.myDatePicker.minimumDate = oneYearFromToday;
self.myDatePicker.maximumDate = twoYearsFromToday;
} @end

iOS开发技巧 - 使用UIDatePicker来选择日期和时间的更多相关文章

  1. iOS开发技巧 - 使用UIPickerView来选择值

    (Swift) import UIKit class ViewController: UIViewController, UIPickerViewDataSource { var picker: UI ...

  2. iOS开发技巧系列---详解KVC(我告诉你KVC的一切)

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...

  3. 【转】几点 iOS 开发技巧

    [译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...

  4. iOS开发技巧

    一.寻找最近公共View 我们将一个路径中的所有点先放进 NSSet 中.因为 NSSet 的内部实现是一个 hash 表,所以查找元素的时间复杂度变成了 O(1),我们一共有 N 个节点,所以总时间 ...

  5. iOS开发技巧系列---使用链式编程和Block来实现UIAlertView

    UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...

  6. iOS开发技巧 -- 复用代码片段

    如果你是一位开发人员在开发过程中会发现有些代码无论是在同一个工程中还是在不同工程中使用率会很高,有经验的人会直接封装在一个类里,或者写成一个宏定义或者把这些代码收集起来,下次直接使用,或者放到xcod ...

  7. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...

  8. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...

  9. iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)

    0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...

随机推荐

  1. 基于设备树的TQ2440 DMA学习(1)—— 芯片手册

    作者 彭东林pengdonglin137@163.com 平台 TQ2440内核Linux4.9 概述 一直想抽时间学习一下DMA驱动,今天就以S3C2440为例,这款芯片的DMA控制器足够简单,也比 ...

  2. EBS应用版本

    SELECT a.release_name FROM apps.fnd_product_groups a;

  3. SpringMVC从Controller跳转到还有一个Controller

    1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带參数跳转.带參数拼接url形式跳转,带參数不拼接參数跳转,页面也能显示. 本来以为挺简单的一件事情. ...

  4. 给js创建的一个input数组绑定click事件

    <html> <body> <input type="button" name="input[]" value="按钮1 ...

  5. Python:Opening Python Classes

    I won’t reply to that post much, because it’s mostly… well, not useful to respond to. But people oft ...

  6. ios之两个view传值

    delegate:demo使用场景,我有A,B两个controller,A是root,跳转到B,B的数据输入完返回A且携带数据显示到A. A.h #import <UIKit/UIKit.h&g ...

  7. Orchard之生成新模板

    一:启用 Code Generation 进入后台, Modules –>  Developer Enable 之.   二:生成模版 首先,进入 Orchard 命令行 在 CMD 下到达解决 ...

  8. 白话Spring(基础篇)---AOP(execution表达式)

    作为AOP的最后一节内容,我们来简单总结一下切面表达式上见的书写方法.下面的那内容有参考其他博文,在此先对开源博客的各位大神表示感谢! -------------------------------- ...

  9. c#使用QQ邮箱的SSL收发邮件

    c#使用SMTP.QQ.COM的SSL验证时,收发邮件,请勿设置端口,代码如下: (1)虽然SSL端口是465,但是,在代码里,不能直接设置端口,很奇怪?挺奇怪,好吧腾讯SSL好像用的是587端口!! ...

  10. 使用python type动态创建类

    使用python type动态创建类 X = type('X', (object,), dict(a=1))  # 产生一个新的类型 X 和下列方法class X(object):    a = 1效 ...