[小白学iOS编程04]UIDatePicker控件学习(键盘处理)

来源:互联网 发布:dcs控制系统编程 编辑:程序博客网 时间:2024/05/13 09:38

注:[小白学iOS编程]是本小白根据某视频学习iOS编程过程的笔记记录,内容比较容易,高手勿喷。
转自请注明原博客地址:http://blog.csdn.net/fan_yufan/article/details/45932421

1. 效果图展示

这里写图片描述

2. 项目代码

2.1 代码编写

////  HMViewController.h//  04-DatePicker(键盘处理)//#import <UIKit/UIKit.h>@interface HMViewController : UIViewController@end
////  HMViewController.m//  04-DatePicker(键盘处理)//#import "HMViewController.h"@interface HMViewController ()@property (weak, nonatomic) IBOutlet UITextField *birthdayFiled;//注意 此次的datePicker用weak有问题,最好用strong@property(nonatomic,weak)UIDatePicker *datePicker;//日期选择@end@implementation HMViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //代码创建UIDatePicker    UIDatePicker *datePicker = [[UIDatePicker alloc] init];    //设置datepicker的本地化    NSArray *idents = [NSLocale availableLocaleIdentifiers];    NSLog(@"%@",idents);    //设置datepicker的本地化为中国    datePicker.locale = [NSLocale                         localeWithLocaleIdentifier:@"zh"];    //设置datepicker模式    datePicker.datePickerMode = UIDatePickerModeDate;//只显示日期,不显示时间    //设置textFiled键盘    self.birthdayFiled.inputView = datePicker;//    UIView *grayView = [[UIView alloc] init];//    grayView.bounds = CGRectMake(0, 0, 320, 44);//    grayView.backgroundColor = [UIColor purpleColor];    //代码创建UIToolbar    UIToolbar *toolbar = [[UIToolbar alloc] init];#warning 一定要设置bounds 否则UIBarButtonItem监听不了点击事件    toolbar.bounds = CGRectMake(0, 0, 320, 44);    toolbar.backgroundColor = [UIColor grayColor];    //创建上一个按钮    UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil];    //创建下一个按钮    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:nil action:nil];    //弹簧    UIBarButtonItem *tanhuangBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];    //创建完成按钮    UIBarButtonItem *finish = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finishSelectedDate)];    //[toolbar setItems:<#(NSArray *)#>]    toolbar.items = @[previous,next,tanhuangBtn,finish];    //设置inputAccessoryView,就能在键盘上面添加辅助的view    self.birthdayFiled.inputAccessoryView = toolbar;    self.datePicker = datePicker;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)finishSelectedDate{    //获取时间    NSDate *selectedDate = self.datePicker.date;    //格式化日期(2014-08-25)    //格式化日期类    NSDateFormatter *formater = [[NSDateFormatter alloc] init];    //设置日期格式    formater.dateFormat = @"yyyy-MM-dd";    NSString *dateStr = [formater stringFromDate:selectedDate];    NSLog(@"%@",dateStr);    //设置textfiled的文本    self.birthdayFiled.text = dateStr;    //隐藏键盘    [self.birthdayFiled resignFirstResponder];}@end

3. 知识点总结

3.1 有时iOS simulator中第一次点击 文本框 未弹出系统键盘是什么原因?
解决方法:点击iOS simulator菜单栏中 Hardware–>Keyboard–>Toggle Software Keyboard.即可解决问题了。

4. 素材内容链接

0 0