在项目中我们经常需要各种各样的弹窗,而系统提供的UIAlertView有的时候并不能满足我们的需求,这个时候需要自定义一个弹窗,我在项目中自定义了一个写的不好,大家见谅
效果图.gif
0.首先创建一个ListAlertView继承UIView
1.声明一个初始化方法
我们先来看苹果给我们提供的初始化方法
UIAlertView *alertView = [UIAlertView alloc] initWithTitle:<#(nullable NSString *)#> message:<#(nullable NSString *)#> delegate:<#(nullable id)#> cancelButtonTitle:<#(nullable NSString *)#> otherButtonTitles:<#(nullable NSString *), ...#>, nil
苹果使用的是类方法,那么我们也按照苹果的格式创建这个自定义控件,因为封装好的控件有可能给其他的同事使用,在没有文档的情况下,对系统提供的方法是最熟悉的,这样可以减少沟通成本,我是这样创建的
+ (instancetype)initWithTitle: (NSString *)title listArray: ( NSArray *)listArray delegate: (id)delegate cancelButtonTitle: (NSString *)cancelButtonTitle otherButtonTitles: (NSString *)otherButtonTitles type: (alertViewType)type cellIndex: (NSInteger)cellIndex;
这个方法不一定是写死了,以后可能需要什么参数可以回来再改
到.m文件中实现这个方法
+ (instancetype)initWithTitle: (NSString *)title listArray: (NSArray *)listArray delegate: (id)delegate cancelButtonTitle: (NSString *)cancelButtonTitle otherButtonTitles: (NSString *)otherButtonTitles type: (alertViewType)type cellIndex: (NSInteger)cellIndex {
ListAlertView *alertView = [[ListAlertView alloc]init];
alertView.delegate = delegate;
alertView.listArray = listArray;
[alertView.title setTitle:title forState:UIControlStateNormal];
[alertView.cancel setTitle:cancelButtonTitle forState:UIControlStateNormal];
alertView.cellIndex = cellIndex;
alertView.alertViewType = type;
[alertView.sure setTitle:otherButtonTitles forState:UIControlStateNormal];
alertView.leaveTypeArray = listArray;
[[[UIApplication sharedApplication].delegate window] addSubview:alertView];
alertView.frame = [UIScreen mainScreen].bounds;
return alertView;
}
方法解释:在初始化方法里我实例化了alertView,它实际上就是整个后面的蒙版,它有一些属性是我声明在.m中的私有变量
@property (nonatomic , strong) UIView *backGround;
@property (nonatomic , strong) UIButton *sure;
@property (nonatomic , strong) UIButton *cancel;
@property (nonatomic , strong) UIButton *title;
@property (nonatomic , strong) UIView *firstLine;
@property (nonatomic , strong) UIView *secondLine;
@property (nonatomic , strong) UIView *threeLine;
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSArray *leaveTypeArray;
@property (nonatomic , assign) alertViewType alertViewType;
@property (nonatomic, strong) UIPickerView *datePicker;
@property (nonatomic , assign) NSInteger cellIndex;
同样的,需要懒加载这些控件
- (UIView *)firstLine {
if (_firstLine == nil) {
_firstLine = [[UIView alloc]init];
_firstLine.backgroundColor = ZCRedColor;
}
return _firstLine;
}
//threeLine
- (UIView *)threeLine {
if (_threeLine == nil) {
_threeLine = [[UIView alloc]init];
_threeLine.backgroundColor = ZCRedColor;
}
return _threeLine;
}
- (UIView *)secondLine {
if (_secondLine == nil) {
_secondLine = [[UIView alloc]init];
_secondLine.backgroundColor = ZCRedColor;
}
return _secondLine;
}
- (UIButton *)title {
if (_title == nil) {
_title = [[UIButton alloc]init];
// NSLog(@"backGround%f",self.backGround.width);
[_title setTitleColor:ZCRedColor forState:UIControlStateNormal];
_title.titleLabel.font = ZCFont16;
}
return _title;
}
//sure
- (UIButton *)cancel {
if (_cancel == nil) {
_cancel = [[UIButton alloc]init];
[_cancel setTitleColor:ZCRedColor forState:UIControlStateNormal];
[_cancel addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _cancel;
}
- (UIButton *)sure {
if (_sure == nil) {
_sure = [[UIButton alloc]init];
[_sure setTitleColor:ZCRedColor forState:UIControlStateNormal];
_sure.titleLabel.font = ZCFont16;
[_sure addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _sure;
}
- (UIView *)backGround {
if (_backGround == nil) {
if (self.alertViewType == alertViewTypeList) {
_backGround = [[UIView alloc]initWithFrame:CGRectMake(0, 0,288, 160)];
} else if (self.alertViewType == alertViewTypeDate) {
_backGround = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth * 0.9, 240)];
}
_backGround.backgroundColor = ZCWhiteColor;
_backGround.layer.cornerRadius = 8;
_backGround.layer.shadowOffset = CGSizeMake(0, 0);
_backGround.layer.shadowOpacity = 1.0;
_backGround.layer.shadowColor = ZCLightGrayColor.CGColor;
_backGround.center = self.center;
}
return _backGround;
}
未完
网友评论