UIDatePicker控件

news/2024/7/2 21:11:29

UIDatePicker是一个可以用来选择或者设置日期的控件,不过它是像转轮一样的控件,而且是苹果专门为日历做好的控件,如下图所示:

除了UIDatePicker控件,还有一种更通用的转轮形的控件:UIPickerView,只不过UIDatePicker控件显示的就是日历,而UIPickerView控件中显示的内容需要我们自己用代码设置。本篇文章简单介绍UIDatePicker控件,后边的文章会介绍UIPickerView。

1、运行Xcode 4.2,新建一个Single View Application,名称为UIDatePicker Test,其他设置如下图所示:

2、单击ViewController.xib,打开Interface Builder。拖一个UIDatePicker控件到视图上:

3、然后拖一个按钮在视图上,并修改按钮名称为Select:

单击按钮后,弹出一个Alert,用于显示用户所作选择。

4、创建映射:打开Assistant Editor,选中UIDatePicker控件,按住Control,拖到ViewController.h中:

新建一个Outlet,名称为datePicker:

然后以同样的方式为按钮建立一个Action映射,名称为buttonPressed,事件类型为默认的Touch Up Inside。

5、选中UIDatePicker控件,打开Attribute Inspector,在其中设置Maximum Date为2100-12-31:

6、单击ViewController.m,找到buttonPressed方法,在其中添加代码如下:

view source
print ?
01- (IBAction)buttonPressed:(id)sender {
02    NSDate *selected = [datePicker date];
03    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
04    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm +0800"];
05    NSString *destDateString = [dateFormatter stringFromDate:selected];
06      
07    NSString *message = [[NSString alloc] initWithFormat: 
08                         @"The date and time you selected is: %@", destDateString]; 
09    UIAlertView *alert = [[UIAlertView alloc] 
10                          initWithTitle:@"Date and Time Selected" 
11                          message:message
12                          delegate:nil 
13                          cancelButtonTitle:@"Yes, I did." 
14                          otherButtonTitles:nil]; 
15    [alert show]; 
16}

其中NSDate *selected = [datePicker date];用于获得UIDatePicker所选择的日期和时间,后边的三行代码把日期和时间改成东八区的时间格式。

找到viewDidLoad方法,添加代码如下:

view source
print ?
1- (void)viewDidLoad
2{
3    [super viewDidLoad];
4    // Do any additional setup after loading the view, typically from a nib.
5    NSDate *now = [NSDate date];
6    [datePicker setDate:now animated:NO]; 
7}

找到viewDidUnload方法,添加代码:

view source
print ?
1- (void)viewDidUnload
2{
3    [self setDatePicker:nil];
4    [super viewDidUnload];
5    // Release any retained subviews of the main view.
6    // e.g. self.myOutlet = nil;
7    self.datePicker = nil;
8}

7、现在运行,显示如下图所示:

 


http://www.niftyadmin.cn/n/3653619.html

相关文章

Spring框架:项目名称起源

From:http://blog.interface21.com/main/2006/11/08/spring-framework-the-origins-of-a-project-and-a-name 我总是经常问,Spring这个名字到底是从何而来.名字要从2002年十一月说起了,我发表了一本书叫<Expert One-on-One J2EE Design and Development.>书里附带了30000…

UIPickerView控件

UIPickerView控件是比UIDatePicker控件更普通的Picker控件&#xff0c;UIDatePicker控件可以理解成是从UIPickerView控件加工出来的专门进行日期选择的控件。 UIPickerView控件的用法比UIDatePicker复杂一点。本文中的小例子将用UIPickerView控件做出两种效果&#xff0c;第一…

Oracle开放Oracle App Server与Spring Framework的集成代码

From:http://blog.interface21.com/main/2007/02/27/oracle-contributing-oracle-application-server-integration-code-to-spring-framework/在应用服务器对Spring进行集成支持的主题方面,又有了新的消息.Oracle已经开始了增加产品Oracle Application Server对Spring集成的工作…

为什么开源产业不同于沃尔玛

From: http://blog.interface21.com/main/2007/03/21/why-open-source-businesses-are-not-like-wal-mart/ 非常幸运的&#xff0c;21世纪&#xff0c;已经有些成功和出众的开源项目取得了成功&#xff0c;但是&#xff0c;有趣的是&#xff0c;我们可以往回…

SUN GlassFish拥抱Spring

From:http://blog.interface21.com/main/2007/02/16/suns-glassfish-embracing-spring/Sun最近在开始开源&#xff0c;用户也开始严肃地对待Sun的开源政策。GlassFish在开源的应用服务器中是一个迟迟来到者&#xff0c;不过好象正在开始吸引众多的关注。重要的是&#xff0c;它…

UITableViewCell - UITableView中cell的边框和背景 .

UITableView是iOS开发中最常用的元素&#xff0c;在平常用的iPhone App中大部分都用到了UITableView&#xff0c;所以你应该知道她的强大了。 需求很简单&#xff0c;就是在一个UITableView里面实现一个不一样的UITableViewCell&#xff0c;如下图里的“切换账号”按钮 正常情…

自定义UITableViewCell的accessoryView 判断哪个Button按下 .

UITableview的开发中经常要自定义Cell右侧的AccessoryView&#xff0c;把他换成带图片的按钮&#xff0c;并在用户Tap时判断出是哪个自定义按钮被按下了。 创建自定义按钮&#xff0c;并设为AccessoryView if (cell nil) { cell [[UITableView alloc] initWithStyle:UITable…

Java实现的插入法建立B+树

我所实现的B树是有关于《数据库系统实现》上的B书算法的实现。利用插入法&#xff0c;我构建出了一个以long型数据作为键值&#xff0c;以Object型数据为指针的B索引树。有关我的程序的说明&#xff1a;&#xff08;1&#xff09;元组数量的取值范围的含义是&#xff1a;本程序…