iOS textField弹出自定义键盘(日期时间、省市、国家)
程序员文章站
2022-05-29 23:50:47
...
弹出选择国家效果图
弹出日期时间效果图
弹出省市效果图
1.选择国家(核心代码)
1.1自定义CountryTF继承自UITextField
CountryTF.h
//
// CountryTF.h
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CountryTF : UITextField
- (void)initWithText;
@end
CountryTF.m
//
// CountryTF.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "CountryTF.h"
#import "CountryView.h"
#import "CountryModel.h"
@interface CountryTF ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, weak) UIPickerView *pick;
@end
@implementation CountryTF
- (void)awakeFromNib
{
[super awakeFromNib];
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
}
- (NSArray *)dataArr
{
if (_dataArr == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
_dataArr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrM = [NSMutableArray array];
for (NSDictionary *dict in _dataArr) {
CountryModel *countryMode = [CountryModel countryWithDict:dict];
[arrM addObject:countryMode];
}
_dataArr = arrM;
}
return _dataArr;
}
// 创始化textField
- (void)setUp
{
UIPickerView *pickView = [[UIPickerView alloc] init];
pickView.delegate = self;
pickView.dataSource = self;
self.inputView = pickView;
self.pick = pickView;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.dataArr.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 75;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
CountryView *countryView = [CountryView countryView];
countryView.model = self.dataArr[row];
return countryView;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
CountryModel *model = self.dataArr[row];
self.text = model.name;
}
- (void)initWithText
{
[self pickerView:self.pick didSelectRow:0 inComponent:0];
}
@end
1.2自定义CountryView继承UIView(用的XIB, 也可以代码创建)
CountryView.h
//
// CountryView.h
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import <UIKit/UIKit.h>
@class CountryModel;
@interface CountryView : UIView
@property (nonatomic, strong) CountryModel *model;
+ (instancetype)countryView;
@end
CountryView.m
//
// CountryView.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "CountryView.h"
#import "CountryModel.h"
@interface CountryView ()
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;
@end
@implementation CountryView
+ (instancetype)countryView
{
return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}
- (void)setModel:(CountryModel *)model
{
_model = model;
self.nameLb.text = model.name;
self.iconImgView.image = [UIImage imageNamed:model.icon];
}
@end
1.3 数据模型文件
//
// CountryModel.h
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CountryModel : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
+ (instancetype)countryWithDict:(NSDictionary *)dict;
@end
//
// CountryModel.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "CountryModel.h"
@implementation CountryModel
+ (instancetype)countryWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
@end
1.选择日期(核心代码)
1.1自定义DateTF继承自UITextField
DateTF.h
//
// DateTF.h
// LayneCheungDemo
//
// Created by llkj on 2017/7/31.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DateTF : UITextField
- (void)initWithText;
@end
DateTF.m
//
// DateTF.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/31.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "DateTF.h"
@interface DateTF ()
@property (nonatomic, weak) UIDatePicker *pick;
@end
@implementation DateTF
- (void)awakeFromNib
{
[super awakeFromNib];
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
}
//初始化
- (void)setUp
{
UIDatePicker *datePick = [[UIDatePicker alloc] init];
//修改datePick日期模式
datePick.datePickerMode = UIDatePickerModeDate;
datePick.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
//监听日期改变
[datePick addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
//日期键盘
self.inputView = datePick;
self.pick = datePick;
}
//当日期改变的时候会调用
- (void)dateChange:(UIDatePicker *)datePick
{
//把当前的日期给文本框赋值
//获取当前选中的日期
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyy-MM-dd"];
//把当前日期转成字符串
self.text = [fmt stringFromDate:datePick.date];
}
- (void)initWithText
{
[self dateChange:self.pick];
}
@end
1.选择省市(核心代码)
1.1自定义CityTF继承自UITextField
CityTF.h
//
// CityTF.h
// LayneCheungDemo
//
// Created by llkj on 2017/7/31.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CityTF : UITextField
- (void)initWithText;
@end
CityTF.m
//
// CityTF.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/31.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "CityTF.h"
#import "CityModel.h"
@interface CityTF ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *dataArr;
/** 当前选中省份的角标 */
@property (nonatomic, assign) NSInteger proIndex;
@property (nonatomic, weak) UIPickerView *pick;
@end
@implementation CityTF
- (void)awakeFromNib
{
[super awakeFromNib];
//初始化
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//初始化
[self setUp];
}
return self;
}
- (NSArray *)dataArr
{
if (_dataArr ==nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
_dataArr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrM = [NSMutableArray array];
for (NSDictionary *dict in _dataArr) {
CityModel *model = [CityModel cityWithDict:dict];
[arrM addObject:model];
}
_dataArr = arrM;
}
return _dataArr;
}
- (void)setUp
{
UIPickerView *pick = [[UIPickerView alloc] init];
pick.delegate = self;
pick.dataSource = self;
self.inputView = pick;
self.pick = pick;
}
//总共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return self.dataArr.count;
} else {
//当前选中的省份决定,当前选中省份下有多少个城市
//当前选中的省份模型,返回当前选中的省份下的城市数量
CityModel *model = self.dataArr[self.proIndex];
return model.cities.count;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
CityModel *model = self.dataArr[row];
return model.name;
} else {
CityModel *model = self.dataArr[self.proIndex];
return model.cities[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
self.proIndex = row;
//第1列选中第0行
[pickerView selectRow:0 inComponent:1 animated:YES];
//刷新数据
[pickerView reloadAllComponents];
}
// 取出省份
CityModel *model = self.dataArr[self.proIndex];
NSString *provinceName = model.name;
// 取出省份对应的城市
// 获取第一列选中的行
NSInteger cityRow = [pickerView selectedRowInComponent:1];
NSString *cityName = model.cities[cityRow];
self.text = [NSString stringWithFormat:@"%@-%@", provinceName, cityName];
}
- (void)initWithText
{
[self pickerView:self.pick didSelectRow:0 inComponent:0];
}
@end
1.2 数据模型文件
//
// CityModel.h
// LayneCheungDemo
//
// Created by llkj on 2017/7/31.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CityModel : NSObject
@property (nonatomic, strong) NSArray *cities;
@property (nonatomic, copy) NSString *name;
+(instancetype)cityWithDict:(NSDictionary *)dict;
@end
//
// CityModel.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/31.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "CityModel.h"
@implementation CityModel
+ (instancetype)cityWithDict:(NSDictionary *)dict
{
CityModel *model = [[CityModel alloc] init];
[model setValuesForKeysWithDictionary:dict];
return model;
}
@end
如何使用(Main.storyboard 拖几个控件,注意绑定对应的类)
//
// ViewController.m
// LayneCheungDemo
//
// Created by llkj on 2017/7/28.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "ViewController.h"
#import "CityTF.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *countryTF;
@property (weak, nonatomic) IBOutlet UITextField *dateTF;
@property (weak, nonatomic) IBOutlet UITextField *cityTF;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.countryTF.delegate = self;
self.dateTF.delegate = self;
self.cityTF.delegate = self;
}
- (void)textFieldDidBeginEditing:(CityTF *)textField
{
//让当前编辑的文本选中第一个.
//在运行时,会去找它真实类型的方法.(在这里可修改textField的类型,也可以给textField写一个category扩充一个initWithText的方法声明)
[textField initWithText];
}
// 拦截用户输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
@end