UIAlertView

来源:互联网 发布:java上机编程题 编辑:程序博客网 时间:2024/05/16 05:13

alertView是ios控件中较为简单的一个,这里实现了一个和有点相同的控件

CheckBoxAlertView.h

#import <UIKit/UIKit.h>/*        效仿UIAlertView */@class CheckBoxAlertView;@protocol CheckBoxAlertViewDelegate <NSObject>-(void)alert:(CheckBoxAlertView *)alert didSelectAtIndex:(NSInteger)index;@end@interface CheckBoxAlertView : UIView//成员属性@property(nonatomic,copy)NSString *title;@property(nonatomic,copy)NSArray *checksArr;@property(nonatomic,weak)id<CheckBoxAlertViewDelegate> delegate;//初始化方法-(instancetype)initWithTitle:(NSString *)title checkTitles:(NSArray *)checkArr delegate:(id)delegate;//获取某一行的标题-(NSString *)checkTitleAtIndex:(NSInteger)index;//展示视图-(void)show;@end

CheckBoxAlertView.m

#import "CheckBoxAlertView.h"#define cellHeight 50//typedef NS_ENUM(NSInteger, subViewSize)//{//    horisnSpace=30,//    //};@interface CheckBoxAlertView (){    UIButton *currentCheckBox;//当前选中的box按钮}@property(nonatomic,strong)UIView *BKView;//背景@property(nonatomic,strong)UILabel *titleLable;//标题@property(nonatomic,strong)UIScrollView *checkList;//容器@property(nonatomic,strong)UIButton *OKButton;//确认@property(nonatomic,strong)UIButton *cancelButton;//取消@end@implementation CheckBoxAlertView-(instancetype)initWithTitle:(NSString *)title checkTitles:(NSArray *)checkArr delegate:(id)delegate{    self=[super initWithFrame:CGRectZero];    if (self) {                _title=title;        _checksArr=checkArr;        _delegate=delegate;                CGFloat horisnSapce=30;        CGFloat viewW=SCREENWIDTH-2*horisnSapce;        CGFloat gap=20;        CGFloat labelW=viewW;        CGFloat labelH=40;        CGFloat cellW=viewW-2*gap;        CGFloat cellH=cellHeight;        CGFloat checkBoxW=30;        CGFloat checkBoxH=checkBoxW;        CGFloat maxlistH=4*cellHeight;        CGFloat buttonW=viewW*0.4;        CGFloat buttonH=labelH;                        _BKView=[[UIView alloc] init];        _BKView.frame=[UIScreen mainScreen].bounds;        [_BKView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.3]];        UITapGestureRecognizer *BKTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOnBKView)];        [_BKView addGestureRecognizer:BKTap];                _titleLable=[[UILabel alloc] init];        _titleLable.frame=CGRectMake(0, 0, labelW, labelH);        _titleLable.text=title;        _titleLable.font=[UIFont fontWithName:@"TrebuchetM" size:25];        _titleLable.textColor=[UIColor redColor];        _titleLable.textAlignment=NSTextAlignmentCenter;        [self addSubview:_titleLable];                _checkList=[[UIScrollView alloc] init];        int count=(int)checkArr.count==0?1:(int)checkArr.count;        _checkList.frame=CGRectMake(gap, CGRectGetMaxY(_titleLable.frame)+gap, cellW, cellH*count>maxlistH?maxlistH:cellH*count);        _checkList.contentSize=CGSizeMake(0, cellH*checkArr.count);        [self addSubview:_checkList];                        _OKButton=[[UIButton alloc] init];        _OKButton.frame=CGRectMake(gap, CGRectGetMaxY(_checkList.frame)+gap, buttonW, buttonH);        [_OKButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        [_OKButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];        [_OKButton setTitle:@"确认" forState:UIControlStateNormal];        [_OKButton addTarget:self action:@selector(clickOnOkButton:) forControlEvents:UIControlEventTouchUpInside];        [_OKButton setBackgroundImage:[[UIImage imageNamed:@"callAnyTime.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)] forState:UIControlStateNormal];        [self addSubview:_OKButton];                _cancelButton=[[UIButton alloc] init];        _cancelButton.frame=CGRectMake(viewW-gap-buttonW, CGRectGetMaxY(_checkList.frame)+gap, buttonW, buttonH);        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        [_cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];        [_cancelButton addTarget:self action:@selector(clickOnCancelButton:) forControlEvents:UIControlEventTouchUpInside];        [_cancelButton setBackgroundImage:[[UIImage imageNamed:@"callAnyTime.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)] forState:UIControlStateNormal];        [self addSubview:_cancelButton];                self.frame=CGRectMake(0, 0, viewW, CGRectGetMaxY(_OKButton.frame)+10);        self.center=CGPointMake(SCREENWIDTH*0.5, SCREENHEIGHT*0.5);        [self setBackgroundColor:[UIColor whiteColor]];        self.layer.cornerRadius=5;        self.layer.masksToBounds=YES;                //向列表中添加元素        if (checkArr.count==0) {            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, cellW, cellH)];            UIImageView *imageView=[[UIImageView alloc] initWithFrame:view.bounds];            imageView.image=[[UIImage imageNamed:@"alert_btn_signal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];            [view addSubview:imageView];                        UILabel *label=[[UILabel alloc] initWithFrame:view.frame];            label.text=@"暂无数据";            label.tintColor=[UIColor grayColor];                        [view addSubview:label];            [_checkList addSubview:view];            [_OKButton removeFromSuperview];            _cancelButton.frame=CGRectMake(gap, CGRectGetMaxY(_checkList.frame)+gap, viewW-2*gap, buttonH);            _OKButton.enabled=NO;        }else if(checkArr.count==1){            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, cellW, cellH)];                        UIImageView *imageView=[[UIImageView alloc] initWithFrame:view.bounds];            imageView.image=[[UIImage imageNamed:@"alert_btn_signal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];            [view addSubview:imageView];                        UILabel *label=[[UILabel alloc] initWithFrame:view.frame];            label.text=checkArr[0];            [view addSubview:label];            [_checkList addSubview:view];            _OKButton.enabled=YES;        }else{            for(int i=0;i<checkArr.count;i++){                UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, i*cellH, cellW, cellH)];                                UIImageView *imageView=[[UIImageView alloc] initWithFrame:view.bounds];                imageView.image=[[UIImage imageNamed:@"alert_btn_signal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];                [view addSubview:imageView];                                UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellW-checkBoxW, cellH)];                label.text=checkArr[i];                [view addSubview:label];                                UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(cellW-checkBoxW, 0, checkBoxW, checkBoxH)];                button.tag=i;                [button setBackgroundImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateNormal];                [button setBackgroundImage:[UIImage imageNamed:@"CL12.png"] forState:UIControlStateSelected];                [button addTarget:self action:@selector(clickOnCheckBox:) forControlEvents:UIControlEventTouchUpInside];                [view addSubview:button];                [_checkList addSubview:view];            }            _OKButton.enabled=NO;        }    }    return self;}-(void)show{    UIWindow *keyWindow=[UIApplication sharedApplication].keyWindow;    [keyWindow addSubview:_BKView];    [keyWindow addSubview:self];}-(void)hide{    [_BKView removeFromSuperview];    [self removeFromSuperview];}-(void)clickOnBKView//退出{    [self hide];}-(void)clickOnOkButton:(UIButton *)button{    NSLog(@"%lu",currentCheckBox.tag);    if ([_delegate respondsToSelector:@selector(alert:didSelectAtIndex:)]) {        [_delegate alert:self didSelectAtIndex:currentCheckBox.tag];    }    [self hide];}-(void)clickOnCancelButton:(UIButton *)button{    [self hide];}-(void)clickOnCheckBox:(UIButton *)button{    if (currentCheckBox) {        currentCheckBox.selected=NO;    }    currentCheckBox=button;    currentCheckBox.selected=YES;    _OKButton.enabled=YES;}-(NSString *)checkTitleAtIndex:(NSInteger)index{    return _checksArr[index];}@end



0 0
原创粉丝点击