UIDatePicker 使用方法

UITextField使用注意事项:

如果使用代码添加UITextField到视图中,默认创建的UITextField是没有边框的,而且文本框的垂直对齐方式需要更改,否则是垂直顶端对齐。

1)设置边框样式

[textField setBorderStyle:UITextBorderStyleRoundedRect];

2)设置文本框内容垂直对齐

 

[textField setContentVerticalAlignment: UIControlContentVerticalAlignmentCenter];

 

UITextField的常用代理方法

// 当文本框开始获得焦点的时候调用

扫描二维码关注公众号,回复: 390123 查看本文章

- (void)textFieldDidBeginEditing:

 

// 当用户市区焦点的时候调用

- (void)textFieldDidEndEditing:

 

// 在文本框准备聚焦的时候调用,返回NO代表不允许聚焦(编辑)

- (BOOL)textFieldShouldBeginEditing:

 

// 在文本框准备失去焦点(退出键盘)的时候调用,返回YES代表允许退出键盘

- (BOOL)textFieldShouldEndEditing:

 

// 在用户每次输入的时候都会调用,返回YES代表允许输入

- (BOOL)textfield: shouldChangeCharactersInRange: replacementString:

 

 

UIDatePicker的用法

//初始化UIDatePicker

UIDatePicker *datePicker = [[UIDatePickeralloc]init]; 

/*

设置UIDatePicker的样式,有四种样式

1) UIDatePickerModeDate

2) UIDatePickerModeDateAndTime

3) UIDatePickerModeTime

4) UIDatePickerModeCountDownTimer

**/

[datePicker setDatePickerMode:UIDatePickerModeDate]; 

// 设置UIDatePicker的locale, locale决定了显示的日期和时间的格式

[datePicker setLocale:[[NSLocalealloc] initWithLocaleIdentifier:@"zh_Hans"]];

// 设置UIDatePicker的响应事件,该响应事件监控值的改变

 

[datePicker addTarget:selfaction:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

// 设置默认时间

 

[self.datePickersetDate:

[dateFormatter dateFromString:dateString]];

 

与UIDatePicker一同使用的是NSDateFormatter, 用NSDateFormatter可以把NSDate转化成NSString,或把NSString转化成NSDate. 用法如下:

NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]init];

/*

设置日期格式

yyyy 年

MM 月

dd 日

hh 小时

mm 分钟

ss 秒

**/

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

 

return [dateFormatter dateFromString:dateString];

猜你喜欢

转载自harveysydney.iteye.com/blog/2236154