今天来写一下关于选择器的使用:
选择器对于我们来说非常常见了,在地址啊,时间啊什么的选择上我们都用到了选择器,之前看很多选择器在选完之后又回到初始状态了,然而一些需求要求保留选择的状态的,所以就自己写了一个
首先创建一个类QZPickerView,继承自UIView,我们的选择器就在这里面实现,在你要展现的地方调用show方法就可以了。
在.h文件中:
//因为是要在控制器和view之间来回传值的所以在这里至少要写两个传值方法
//1.将控制器中的值传给选择器 选择器出现的时候之间在要选的值上
- (void)showPickerViewFirstStr:(NSString *)firstStr;
//2.将选择器中选好的值传给控制器 (这个有好几种方法 可以用代理、block和通知都可以 看个人习惯 在这里我用的代理)
@class QZPickerView;
@protocol QZPickerViewDelegate <NSObject>
- (void)pickerView:(QZPickerView *)pickerView firstStr:(NSString *)firstStr;
@interface QZPickerView : UIView
@property (nonatomic, weak) id<QZPickerViewDelegate> delegate;
@end
.m中:
#import "QZPickerView.h"
#define kViewW self.frame.size.width
#define kViewH self.frame.size.height
@interface QZPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *firstArr;
@property (nonatomic, strong) NSArray *secArr;
@property (nonatomic, strong) NSArray *thirArr;
@property (nonatomic, assign) NSInteger firstCurrentIndex;
@property (nonatomic, assign) NSInteger secondCurrentindex;
@property (nonatomic, assign) NSInteger thirdCurrentindex;
@end
@implementation QZPickerView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupSubViews];
}
return self;
}
- (void)setupSubViews
{
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kViewH *0.5 - 20, kViewW, 40)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"时间设置";
[self addSubview:titleLabel];
[self addSubview:self.pickerView];
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(0, kViewH - 60, kViewW *0.5-0.5, 60);
[self addSubview:button];
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"取消" forState:UIControlStateNormal];
[button addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [[UIButton alloc] init];
button2.frame = CGRectMake(kViewW *0.5+0.5, kViewH - 60, kViewW *0.5, 60);
[self addSubview:button2];
button2.backgroundColor = [UIColor grayColor];
[button2 setTitle:@"确定" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(completionBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)show
{
self.hidden = NO;
}
- (void)hide
{
self.hidden = YES;
}
- (void)completionBtnClick
{
NSString *firStr = @"";
if (!self.firstCurrentIndex) {
self.firstCurrentIndex = 0;
}
firStr = self.firstArr[self.firstCurrentIndex];
[self hide];
if ([self.delegate respondsToSelector:@selector(pickerView:firstStr:)]) {
[self.delegate pickerView:self firstStr:firStr];
}
}
- (void)showPickerViewFirstStr:(NSString *)firstStr
{
self.firstArr = kDataManager.dataArray;
if (firstStr.length >0) {
for (NSInteger i = 0; i < self.firstArr.count; i++) {
if ([self.firstArr[i] isEqualToString:firstStr]) {
self.firstCurrentIndex = i;
}
}
}
[self.pickerView selectRow:self.firstCurrentIndex inComponent:0 animated:NO];
[self show];
}
#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated {
return;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return self.firstArr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *str = self.firstArr[row];
return str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.firstCurrentIndex = row;
//这个是当有多级联动时用来刷新数据的
// [self.pickerView reloadAllComponents];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
pickerLabel.adjustsFontSizeToFitWidth = YES;
[pickerLabel setTextAlignment:NSTextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont systemFontOfSize:16]];
}
// Fill the label text here
pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
- (UIPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kViewH *0.5 + 30, kViewW, kViewH *0.3)];
_pickerView.delegate = self;
_pickerView.dataSource = self;
[_pickerView selectRow:0 inComponent:0 animated:NO];
}
return _pickerView;
}
剩下的就是在控制器中实现了:引入
#import "QZPickerView.h"
声明
@property (nonatomic, strong) QZPickerView *pickerView;
懒加载
- (QZPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[QZPickerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_pickerView];
//遵守代理
_pickerView.delegate = self;
}
return _pickerView;
}
实现代理方法
- (void)pickerView:(QZPickerView *)pickerView firstStr:(NSString *)firstStr
{
NSLog(@"%s",__func__);
}
加载方法:
- (void)show:(UIView *)sender
{
NSString *firstStr = @"22";
[self.pickerView showPickerViewFirstStr:firstStr];
}
完成!!!