iOS开发技巧 - 使用UIDatePicker来选择日期和时间

(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 = 365 * 24 * 60 * 60
        let todayDate = NSDate()
        let oneYearFromToday = todayDate.dateByAddingTimeInterval(oneYearTime)
        let twoYearsFromToday = todayDate.dateByAddingTimeInterval(2 * 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 = 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;
}

@end

 

时间: 2024-10-28 08:30:57

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

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

(Swift) import UIKit class ViewController: UIViewController, UIPickerViewDataSource { var picker: UIPickerView! override func viewDidLoad() { super.viewDidLoad() picker = UIPickerView() // select the current view controller as the data source of the

手势-iOS 8中自定义的继承于UIActionSheet的UIDatePicker,无法滑动选择日期和时间

问题描述 iOS 8中自定义的继承于UIActionSheet的UIDatePicker,无法滑动选择日期和时间 iOS 8中自定义的UIDatePicker(继承于UIActionSheet),无法滑动选择日期和时间,自定义的datepicker显示在View上,而View显示在window上,如果写了touchesBegan方法,则会消失,总之无法响应手势事件了,求解!!!

iOS开发技巧之查看模拟器沙盒文件

iOS开发技巧之查看模拟器沙盒文件 iOS开发中,在对数据库进行操作时,有时我们需要直观的查看数据库的内容,那么我们如何找到沙盒中的这个文件呢,步骤很简单: 1.点击Finder选项栏上的前往菜单: 2.选择前往文件夹选项: 前往的文件路径为:/Users/username/Library/Application Support/iPhone Simulator/ 其中username为当前mac电脑的用户名. 3.界面类似如下模样,选择一个版本的模拟器,应用的沙盒文件就在Application

js-求Ext5自定义datetimefield,要求可选择日期和时间,时分秒可选择

问题描述 求Ext5自定义datetimefield,要求可选择日期和时间,时分秒可选择 如题,要求用ExtJS5自定义datetimefield控件,可选择时分秒,官方api没有提供这控件,网上百度的Ext4.2的大牛自己写的在Ext5中不可用 有大牛研究出来的请发源码到564892076@qq.com 解决方案 http://hyssmt.blog.51cto.com/4151829/1652212

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

0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕适配问题了, 因此, 在iOS 8以后苹果推出了Size Class, 它是基于Auto Layout技术的.   1. Size Class的开启和使用 与Auto Layout不同, Size Class不能通过代码编程管理,只能通过IB(Interface Builder)使用. 默认情况下S

js选择日期_时间日期

function dateSelector() //构造dateSelector对象,用来实现一个日历形式的日期输入框. { var myDate=new Date(); this.year=myDate.getYear(); //定义year属性,年份,默认值为当前系统年份. this.month=myDate.getMonth()+1; //定义month属性,月份,默认值为当前系统月份. this.date=myDate.getDate(); //定义date属性,日,默认值为当前系统的日

iOS开发技巧 - 使用UISlider来调整值的范围

(Swift) import UIKit class ViewController: UIViewController { var slider: UISlider! func sliderValueChanged(slider: UISlider) { println("Slider's new value is \(slider.value)") } override func viewDidLoad() { super.viewDidLoad() slider = UISlide

iOS开发技巧 - 使用Alerts和Action Sheets显示弹出框

解决方案: (Swift) 使用UIAlertController类   (Objective-C) 使用UIAlertView类   代码: (Swift) import UIKit class ViewController: UIViewController { // 1. define the variable that will hold our alert controller var controller:UIAlertController? override func viewDi

iOS开发技巧 - 使用UISegmentedControl来对信息进行分组

      (Swift) import UIKit class ViewController: UIViewController { var segmentedControl:UISegmentedControl! override func viewDidLoad() { super.viewDidLoad() let segments = [ "iPhone", "iPad", "iPod", "iMac"] segme