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

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

1. 效果图展示

2. 项目代码

2.1 代码编写

(weak, nonatomic) IBOutlet UITextField *birthdayFiled;- (void)viewDidLoad{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//代码创建UIDatePickerUIDatePicker *datePicker = [[UIDatePicker alloc] init];//设置datepicker的本地化NSArray *idents = [NSLocale availableLocaleIdentifiers];NSLog(@”%@”,idents);//设置datepicker的本地化为中国datePicker.locale = [NSLocalelocaleWithLocaleIdentifier:@”zh”];//设置datepicker模式datePicker= datePicker;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];= toolbar;self.datePicker = datePicker;}- (void)didReceiveMemoryWarning{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}-(void)finishSelectedDate{;//格式化日期(2014-08-25)//格式化日期类NSDateFormatter *formater = [[NSDateFormatter alloc] init];//设置日期格式formater.dateFormat = @”yyyy-MM-dd”;NSString *dateStr = [formater stringFromDate:selectedDate];NSLog(@”%@”,dateStr);= dateStr;//隐藏键盘[self.birthdayFiled resignFirstResponder];}@end3. 知识点总结

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

4. 素材内容链接

人创造奇迹常常是在瞬间,

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

相关文章:

你感兴趣的文章:

标签云: