IOS学习之UIDatePicker控件使用
IOS上的選擇時(shí)間日期的控件是這樣的,左邊是時(shí)間和日期混合,右邊是單純的日期模式。
,??
您可以選擇自己需要的模式,Time, Date,Date and Time ?, Count Down Timer四種模式。
本篇文章簡(jiǎn)單介紹下PickerDate控件的使用
1、新建一個(gè)Singe View Application,命名為DatePickDemo,其他設(shè)置如圖
2、放置控件
打開(kāi)ViewController.xib,拖拽一個(gè)DatePicker控件放到界面上,再拖拽一個(gè)Button控件放到界面上,雙擊Button,輸入"選擇日期時(shí)間"
3、建立xib和ViewController的關(guān)聯(lián)
按下command+alt+enter鍵打開(kāi)Assistant Editor,選中DatePicker按住Control鍵,拖拽到viewController.h上,
建立Outlet datePicker。
以同樣方式給Button建立一個(gè)Action關(guān)聯(lián)映射,命名為selectDate,事件類型為默認(rèn)的Touch?Up?Inside。
4、實(shí)現(xiàn)代碼
單擊ViewController.m,找到剛才創(chuàng)建的- (IBAction)selectDate:(id)sender {
}
在這里添加響應(yīng)代碼
- (IBAction)selectDate:(id)sender { NSDate *select = [datePicker date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *dateAndTime = [dateFormatter stringFromDate:select]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"時(shí)間提示" message:dateAndTime delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alert show]; }運(yùn)行看效果:
5、修改模式成Date模式,修改代碼
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
例子代碼:https://github.com/schelling/YcDemo
著作權(quán)聲明:本文由http://blog.csdn.net/totogo2010/原創(chuàng),歡迎轉(zhuǎn)載分享。請(qǐng)尊重作者勞動(dòng),轉(zhuǎn)載時(shí)保留該聲明和作者博客鏈接,謝謝轉(zhuǎn)載于:https://www.cnblogs.com/deve/archive/2012/06/21/2569074.html
總結(jié)
以上是生活随笔為你收集整理的IOS学习之UIDatePicker控件使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: NPOI 1.2教程(目录)
- 下一篇: ABP开发框架前后端开发系列---(9)