当前位置:网站首页>选择器的使用
选择器的使用
2022-07-26 09:22:00 【~轻舟~】
今天来写一下关于选择器的使用:
选择器对于我们来说非常常见了,在地址啊,时间啊什么的选择上我们都用到了选择器,之前看很多选择器在选完之后又回到初始状态了,然而一些需求要求保留选择的状态的,所以就自己写了一个
首先创建一个类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];
}完成!!!
边栏推荐
猜你喜欢

Object 的Wait Notify NotifyAll 源码解析

redis原理和使用-基本特性

redis原理和使用-安装和分布式配置

js在控制台输出菱形

垂直搜索

2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation

2022 chemical automation control instrument operation certificate test question simulation test platform operation

【Mysql】redo log,undo log 和binlog详解(四)

【Mysql】认识Mysql重要架构(一)
![[MySQL] how to execute an SQL statement (2)](/img/7b/53f8756458cc318e2f417b1cc0c3f8.png)
[MySQL] how to execute an SQL statement (2)
随机推荐
Unity topdown character movement control
PHP和MySQL获取week值不一致的处理
jvm命令归纳
OFDM 十六讲- OFDM
李沐d2l(四)---Softmax回归
微信小程序学习笔记1
"No input file specified" problem handling
MySQL transaction
【线上问题】Timeout waiting for connection from pool 问题排查
arc-gis的基本使用2
Qtcreator reports an error: you need to set an executable in the custom run configuration
NTT(快速数论变换)多项式求逆 一千五百字解析
What are CSDN spaces represented by
Pat grade a A1034 head of a gang
省政府召开全省高温天气安全防范工作电视电话会议
Sliding window, double pointer, monotone queue, monotone stack
Thread Join 和Object wait 的区别
opencv图像处理
大二上第五周学习笔记
Conditions for JVM to trigger minor GC