当前位置:网站首页>Use of selectors
Use of selectors
2022-07-26 09:49:00 【~ light boat ~】
Today, let's write about the use of selectors :
Selectors are very common to us , At the address , We all use selectors in the choice of time , Previously, I saw that many selectors return to the initial state after selection , However, some requirements require that the selected state be preserved , So I wrote one myself
First create a class QZPickerView, Inherited from UIView, Our selector is implemented here , Call where you want to show show The method is ok .
stay .h In file :
// Because it is between the controller and view So at least two value transfer methods should be written here
//1. Pass the value in the controller to the selector When the selector appears, it is on the value to be selected
- (void)showPickerViewFirstStr:(NSString *)firstStr;
//2. Pass the selected value in the selector to the controller ( There are several ways to do this You can use a proxy 、block And notice are ok Personal habits The agent I use here )
@class QZPickerView;
@protocol QZPickerViewDelegate <NSObject>
- (void)pickerView:(QZPickerView *)pickerView firstStr:(NSString *)firstStr;
@interface QZPickerView : UIView
@property (nonatomic, weak) id<QZPickerViewDelegate> delegate;
@end.m in :
#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 = @" Set the time ";
[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:@" Cancel " 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:@" determine " 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;
// This is used to refresh data when there is multi-level linkage
// [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;
}
The rest is implemented in the controller : introduce
#import "QZPickerView.h"Statement
@property (nonatomic, strong) QZPickerView *pickerView;Lazy loading
- (QZPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[QZPickerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_pickerView];
// Compliance agency
_pickerView.delegate = self;
}
return _pickerView;
}Implementing agent methods
- (void)pickerView:(QZPickerView *)pickerView firstStr:(NSString *)firstStr { NSLog(@"%s",__func__); }Loading method :
- (void)show:(UIView *)sender
{
NSString *firstStr = @"22";
[self.pickerView showPickerViewFirstStr:firstStr];
}complete !!!
边栏推荐
- Gauss elimination solves the inverse of matrix (Gauss)
- copyTo
- matlab中的AR模型短时预测交通流
- 2022 zhongkepan cloud - server internal information acquisition and analysis flag
- Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
- How to add a PDB
- QT handy notes (VI) -- update interface, screenshot, file dialog box
- Interpretation of the standard of software programming level examination for teenagers_ second level
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- 服务器、客户端双认证
猜你喜欢

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning

Solve NPM -v sudden failure and no response

R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)

Gauss elimination

服务发现原理分析与源码解读

高斯消元求解矩阵的逆(gauss)

Gauss elimination solves the inverse of matrix (Gauss)

小白搞一波深拷贝 浅拷贝

苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收

I finished watching this video on my knees at station B
随机推荐
In the same CONDA environment, install pytroch first and then tensorflow
Great reward for interview questions
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
“互联网+”时代的现代医学
MFC handy notes
【荧光字效果】
JS 连等赋值操作
IIS error prompt after installing Serv-U: hresult:0x80070020
服务器环境配置全过程
Wechat H5 payment on WAP, for non wechat browsers
[information system project manager] summary of essence of high-level series for the first time
POJ 1012 Joseph
Gauss elimination solves the inverse of matrix (Gauss)
I finished watching this video on my knees at station B
Keeping alive to realize MySQL automatic failover
网络流学习笔记
copyTo
asp. Net using redis cache