当前位置:网站首页>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 !!!
边栏推荐
- R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
- Basic knowledge of website design
- Uni app learning summary
- Nodejs service background execution (forever)
- ie7设置overflow属性失效解决方法
- 解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
- asp. Net using redis cache
- JS continuous assignment operation
- 2019 ICPC Asia Yinchuan regional (water problem solution)
猜你喜欢

Gauss elimination solves the inverse of matrix (Gauss)

After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"

2022年中科磐云——服务器内部信息获取 解析flag

Registration module use case writing

QT handy notes (III) use qtcharts to draw a line chart in VS

I finished watching this video on my knees at station B

Fiddler download and installation

Search module use case writing
![[fluorescent character effect]](/img/05/4f4c24c787881e073919b63a6864cf.jpg)
[fluorescent character effect]

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
随机推荐
The use of MySQL in nodejs
Logical architecture of MySQL
解释一下自动装箱和自动拆箱?
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
asp. Net using redis cache (2)
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
Principle analysis and source code interpretation of service discovery
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
MFC handy notes
Fiddler packet capturing tool for mobile packet capturing
小白搞一波深拷贝 浅拷贝
spolicy请求案例
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
在Blazor 中自定义权限验证
QT handy notes (VI) -- update interface, screenshot, file dialog box
Show default image when wechat applet image cannot be displayed
Double authentication of server and client
Basic knowledge of website design