当前位置:网站首页>选择器的使用
选择器的使用
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];
}
完成!!!
边栏推荐
- js在控制台输出菱形
- 围棋智能机器人阿法狗,阿尔法狗机器人围棋
- Pat grade a a1076 forwards on Weibo
- Laravel框架日志文件存放在哪里?怎么用?
- NTT (fast number theory transformation) polynomial inverse 1500 word analysis
- Windows通过命令备份数据库到本地
- 2022 chemical automation control instrument operation certificate test question simulation test platform operation
- jvm命令归纳
- 安卓 实现缓存机制,多种数据类型缓存
- Order based evaluation index (especially for recommendation system and multi label learning)
猜你喜欢
随机推荐
Qt | 关于如何使用事件过滤器 eventFilter
Summary of common activation functions for deep learning
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
839. Simulation reactor
What is asynchronous operation
自定义密码输入框,无圆角
您的登录IP不在管理员配置的登录掩码范围内
js在控制台输出菱形
Windows backs up the database locally by command
756. Serpentine matrix
[MySQL] detailed explanation of redo log, undo log and binlog (4)
Byte buffer stream & character stream explanation
【Mysql】认识Mysql重要架构(一)
QT | about how to use EventFilter
Processing of inconsistent week values obtained by PHP and MySQL
Elastic APM installation and use
756. 蛇形矩阵
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
Li Mu D2L (IV) -- softmax regression
Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points