介绍UIPickView和UIDatePicker
1.UIPickView什么时候用?
- 通常在注册模块,当用户需要选择一些东西的时候,比如说城市,往往弹出一个PickerView给他们选择。
- 老虎机效果
2.UIPickView常见用法
- 独立的,没有任何关系 => 菜单系统。
- 相关联的,下一列和第一列有联系=> 省会城市选择
- 图文并帽, => 国旗选择。
3.UIPickView
4.UIDatePicker什么时候用?
- 当用户选择日期的时候,一般弹出一个UIDatePicker给用户选择。
5.UIDatePicker iOS6和iOS7的区别
UIPickView与UIDatePicker使用
UIPickView效果图
plist文件
实现过程
- 1.1:设置_pickerView的数据源及代理为控制器
- 1.2:控制器遵守数据源,代理协议
- 1.3:实现数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return self.foods.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *arr = self.foods[component];
return arr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *arr = self.foods[component];
return arr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"%ld %ld",component, row);
NSString *title = self.foods[component][row];
_labelView.text = title;
}
// 返回第component列有多宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
// 返回第component列有高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
// NSAttributedString:属性字符串(给文本添加一些属性),富文本,丰富文本,可以设置文字的颜色,字体,阴影,空心,图文混排
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented
// 返回第component列第row行视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
UIPickView与UIDatePicker的综合使用
效果图
plist文件
实现过程
国旗文本框
- 1.1:自定义xib
- 1.2:创建模型Flag
- 1.3:创建继承UITextField的国旗FlagField
#import "FlagField.h"
#import "Flag.h"
#import "FlagView.h"
@interface FlagField ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, strong) NSMutableArray *flags;
@end
@implementation FlagField
- (NSMutableArray *)flags
{
if (_flags == nil) {
_flags = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:filePath];
for (NSDictionary *dict in dictArr) {
Flag *falg = [Flag flagWithDict:dict];
[_flags addObject:falg];
}
}
return _flags;
}
- (void)awakeFromNib
{
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUp];
}
return self;
}
- (void)setUp
{
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
self.inputView = pickerView;
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.flags.count;
}
#pragma mark - UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
FlagView *flagV = [FlagView flagView];
flagV.flag = self.flags[row];
return flagV;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 100;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
Flag *flag = self.flags[row];
self.text = flag.name;
}
- (void)setUpText
{
[self pickerView:nil didSelectRow:0 inComponent:0];
}
出生年月文本框
- 2.1:创建继承UITextField的出生年月BirthdayField
#import "BirthdayField.h"
@implementation BirthdayField
- (void)awakeFromNib
{
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUp];
}
return self;
}
- (void)setUp
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *date = [fmt dateFromString:@"1990-1-1"];
datePicker.date = date;
[datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
self.inputView = datePicker;
}
- (void)dateChange:(UIDatePicker *)datePicker
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *dateStr = [fmt stringFromDate:datePicker.date];
self.text = dateStr;
}
- (void)setUpText
{
[self dateChange:(UIDatePicker *)self.inputView];
}
城市文本框
- 3.1:创建模型Province
- 3.2:创建继承UITextField的城市CityField
#import "CityField.h"
#import "Province.h"
@interface CityField () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, assign) NSInteger selProvinceIndex;
@property (nonatomic, strong) NSMutableArray *provinces;
@end
@implementation CityField
- (NSMutableArray *)provinces
{
if (_provinces == nil) {
_provinces = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:filePath];
for (NSDictionary *dict in dictArr) {
Province *p = [Province provinceWithDict:dict];
[_provinces addObject:p];
}
}
return _provinces;
}
- (void)awakeFromNib
{
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUp];
}
return self;
}
- (void)setUp
{
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
self.inputView = pickerView;
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return self.provinces.count;
}else{
Province *p = self.provinces[_selProvinceIndex];
return p.cities.count;
}
}
#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
Province *p = self.provinces[row];
return p.name;
}else{
Province *p = self.provinces[_selProvinceIndex];
return p.cities[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
_selProvinceIndex = row;
[pickerView reloadComponent:1];
}
Province *p = self.provinces[_selProvinceIndex];
NSString *pName = p.name;
NSInteger cityIndex = [pickerView selectedRowInComponent:1];
NSString *cityName = p.cities[cityIndex];
self.text = [NSString stringWithFormat:@"%@ %@",pName,cityName];
}
- (void)setUpText
{
[self pickerView:nil didSelectRow:0 inComponent:0];
}
@end
控制器
#import "ViewController.h"
#import "UITextField+Text.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *flagField;
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
@property (weak, nonatomic) IBOutlet UITextField *cityField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_flagField.delegate = self;
_birthdayField.delegate = self;
_cityField.delegate = self;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setUpText];
}
@end
为UITextField创建一个分类,添加一个设置方法
#import
@interface UITextField (Text)
- (void)setUpText;
@end