UIDataPicker

在storyBoard中直接选择对应的语言,日期格式

用代码方式显示:

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

NSLog(@"%@",[NSLocale availableLocaleIdentifiers] );

//显示日期控制的本地化

self.myDataPicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh"];

}

=======固定的弹簧,可拉伸的弹簧

#import "ViewController.h"

#import "CZKeyboardToolbar.h"

@interface ViewController ()<CZKeyboardToolbarDelegate>

@property (strong, nonatomic) UIDatePicker *datepicker;

@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//创建datapikcer

self.datepicker = [[UIDatePicker alloc] init];

//本地方

self.datepicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];

//日期控件格式

self.datepicker.datePickerMode = UIDatePickerModeDate;

//设置textfield的键盘

self.textField.inputView = self.datepicker;

CZKeyboardToolbar *toolbar = [CZKeyboardToolbar toolbar];

//设置键盘的代理

toolbar.kbDelegate = self;

//设置textfield的辅助工具条

self.textField.inputAccessoryView = toolbar;

}

#pragma mark 自定义键盘工具条的代理方法

-(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item{

if (item.tag == 2) {//Done按钮点击

//获取日期显示在textField

NSDate *date = self.datepicker.date;

NSLog(@"%@",date);

//日期转字符串

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//设置日期格式

dateFormatter.dateFormat = @"yyyyMMdd";

NSString *dateStr =  [dateFormatter stringFromDate:date];

self.textField.text = dateStr;

}

}

/**

*  代码创建的toolbar

*/

-(void)codeForToolbar{

//代码创建UIToolbar

UIToolbar *toolbar = [[UIToolbar alloc] init];

toolbar.backgroundColor = [UIColor grayColor];

//屏幕宽度

CGFloat screenW = [[UIScreen mainScreen] bounds].size.width;

toolbar.bounds = CGRectMake(0, 0, screenW, 44);

UIBarButtonItem *previousBtn = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil];

UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:nil action:nil];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:nil action:nil];

//固定长度的按钮

UIBarButtonItem *fixedBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

//代码实现要设置宽度

fixedBtn.width = 10;

//可拉伸的按钮

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

//添加UIToolbar里面的按钮

toolbar.items = @[previousBtn,fixedBtn,nextBtn,flexible,doneBtn];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

时间: 2024-08-14 17:56:23

UIDataPicker的相关文章

UIDataPicker 时间选择器

自用时间选择器 1 @interface ViewController () 2 { 3 UILabel *cityLabel; 4 UIDatePicker *datePicker; 5 } 6 //@property(nonatomic,strong)ZHPickView *pickview; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 //

IOS基础面试题

最近离职了,找工作,光会做项目,对基础不熟,今天就总结了一点面试题. 废话不多说,上题吧: 1.objective-c中的数字对象都有哪些,简述它们与基本数据类型的区别是什么. 基本类型和C一样,主要是有int.long.double.float.char.void.bool.对于基本数据类型,不需要使用指针,NSNumber是OC的数字对象,需要考虑内存释放问题.数字类型有:NSInteger.CGFloat.数据对象有NSNumber.对象和变量的差别.可以拆装效果.其他的类型有NSStri

UIDatePicker,UIPickerView,MultiComponentPicker

UIDatePicker 属性: Mode:Date and Time Locale: Interval: 最大值,最小值 NSDate *date=[self.dataPicker date]; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *date2=[formatter stringFromD

iOS Programming View Controllers 视图控制器

iOS Programming View Controllers? 视图控制器? 1.1? A view controller is an instance of a subclass of UIViewController. 一个view controller 是一个UIViewController的子类. A view controller manages a view hierarchy. 一个view controller 管理一个视图树. It is responsible for c

ios自定义选择器ActionSheetPicker改进版

ios自带的UIDataPicker和UIDatePicker最大的毛病就是没有带确定和取消这两个按钮,而ActionSheetPicker是以上两个选择器的开源封装.但是这个东东也有些小问题,就是没有汉化选择器的内容. 原始的ActionSheetPicker运行起来是这个样子的: 改进后是这以下这个样子的 点击此处下载,有问题可加扣扣359709421讨论. 补充一个缺陷的修复,在ios7以上版本中,选择器数据部份是透明的,很不美观, 把AbstractActionSheetPicker.m