自定义弹窗(OC)

在工作中,系统自带的弹窗不太好用,弹窗中间还没法添加textView等控件,所以晚上做了一个(熊猫眼是有原因的)…
说明:封装了一个类alertView,实例化对象的时候,调用自定义的init方法会直接要求传入数据和成为alertView的代理,代理的作用是在弹窗中做完事务后,返回数据…
下面直接上代码:
#import <UIKit/UIKit.h>
//实现代理方法,获得点击"确定"后的返回数据
@protocol alertDelegate <NSObject>
@required
-(void)alertCallBackDataWithDict:(NSMutableDictionary *)dict;
@end

@interface alertView : UIViewController

@property(weak ,nonatomic)id<alertDelegate> delegate;
-(void)showWithViewController:(UIViewController *)viewController;
-(instancetype)initWithArray:(NSArray *)array delegate:(id) delegate;
@end
下面是.m中代码:
还有很多地方存在问题,比如:适配问题…
//使用该类只需要实现下面两步:
//1.实例化对象
//2.show

#import "alertView.h"
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

@interface alertView ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>

@property(weak, nonatomic) UIView * alertView;
@property(weak, nonatomic) UILabel * headLabel;
@property(weak, nonatomic) UITextField * textF;
@property(weak, nonatomic) UITextView * textView;
@property(weak, nonatomic) UITableView * tableView;
@property(strong, nonatomic) NSMutableDictionary * dict;
@property(strong, nonatomic) UIViewController * viewController;
@property(strong, nonatomic)NSArray * alertArray;

@end

@implementation alertView
-(NSMutableDictionary *)dict{
    if (_dict == nil) {
        _dict = [NSMutableDictionary dictionary];
    }
    return _dict;
}
-(instancetype)initWithArray:(NSArray *)array delegate:(id)delegate{
    if (self = [super init]) {
        self.alertArray = array;
        self.delegate = delegate;
    }
    return self;
}
static NSString * const IDAlert = @"IDAlert";
-(void)viewDidLoad{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:IDAlert];
}


#pragma 显示弹窗
-(void)showWithViewController:(UIViewController *)vc{
    [vc.view addSubview:self.view];
    [vc addChildViewController:self];
    UIView * backView = [self addBackView];
    UIView * alertView = [self addAlertView:backView];
    [self addAllSubView:alertView];
    self.viewController = vc;
}
#pragma 添加弹窗的底部蒙板
-(UIView *)addBackView{
    UIView * backView = self.view;
    backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    UIViewController * vc =  [UIApplication sharedApplication].keyWindow.rootViewController;
    //如果有navBar,设置蒙板的y值
    CGFloat navBarheight = vc.navigationController.navigationBar.frame.size.height;
    backView.frame = CGRectMake(0, navBarheight, ScreenW, ScreenH - navBarheight);

    backView.center = CGPointMake(ScreenW * 0.5 , ScreenH * 0.5);


    return backView;
}
#pragma 添加弹窗
-(UIView *)addAlertView:(UIView *)backView{
    UIView * alertView = [[UIView alloc] init];
    alertView.backgroundColor = [UIColor whiteColor];
    CGFloat alertW = 5.0 / 7 * [UIScreen mainScreen].bounds.size.width;
    CGFloat alertY = 100;
    alertView.frame = CGRectMake((ScreenW - alertW) * 0.5, alertY, alertW, 310);
    [backView addSubview:alertView];
    self.alertView = alertView;
    return alertView;
}
#pragma 添加子控件
-(void)addAllSubView:(UIView *)alertView{
    //1.
    UILabel * headLabel = [[UILabel alloc] init];
    CGFloat labelX = 0;
    CGFloat labely = 0;
    CGFloat labelW = alertView.bounds.size.width;
    CGFloat labelH = 40;
    CGFloat margin = 10;

    headLabel.frame = CGRectMake( labelX, labely, labelW, labelH);
    headLabel.textAlignment = NSTextAlignmentCenter;
    headLabel.text = @"客服派单";
    headLabel.font = [UIFont systemFontOfSize:20];
    headLabel.textColor = [UIColor blackColor];
    self.headLabel = headLabel;
    [alertView addSubview:headLabel];

    //2.
    UIView *lineView = [[UIView alloc] init];
    CGFloat lineX = margin;
    CGFloat lineW = labelW - margin * 2;
    lineView.frame = CGRectMake(margin , labelH, lineW, 1);
    lineView.backgroundColor = [UIColor colorWithRed:68.0/255 green:82.0/255 blue:93.0/255 alpha:1];
    [alertView addSubview:lineView];

    //3.
    UITextField * textF = [[UITextField alloc] init];
    CGFloat textFH = 40;
    textF.frame = CGRectMake(lineX, textFH + margin, lineW, textFH);
    textF.placeholder = @"请输入接受人";
    //边框设置为UITextBorderStyleNone,好看的边框效果可以设置为textF的setBackground图片;
    textF.borderStyle = UITextBorderStyleRoundedRect;
    [alertView addSubview:textF];
    textF.delegate = self;

    self.textF = textF;

    UITextView * textView = [[UITextView alloc] init];
    CGFloat textVY = CGRectGetMaxY(textF.frame) + margin;
    CGFloat textVH = 155;
    textView.frame = CGRectMake(lineX, textVY, lineW, textVH);
    textView.backgroundColor = [UIColor colorWithRed:220.0/255 green:220.0/255 blue:220.0/255 alpha:1];
    [alertView addSubview:textView];
    self.textView = textView;

    UIButton * leftBtn = [[UIButton alloc] init];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setBackgroundColor:[UIColor colorWithRed:30.0/255 green:82.0/255 blue:174.0/255 alpha:1]];
    CGFloat btnH = 40;
    CGFloat btnW = (alertView.bounds.size.width - 3 * margin) * 0.5;
    CGFloat btnx = margin;
    CGFloat btny = alertView.bounds.size.height - margin - btnH;
    leftBtn.frame = CGRectMake(btnx, btny, btnW, btnH);
    [alertView addSubview:leftBtn];

    UIButton * rightBtn = [[UIButton alloc] init];
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setBackgroundColor:[UIColor colorWithRed:30.0/255 green:82.0/255 blue:174.0/255 alpha:1]];
    CGFloat rightX = CGRectGetMaxX(leftBtn.frame) + margin;
    rightBtn.frame = CGRectMake(rightX, btny, btnW, btnH);
    [alertView addSubview:rightBtn];

    [leftBtn addTarget:self action:@selector(leftTest) forControlEvents:UIControlEventTouchDown];
    [rightBtn addTarget:self action:@selector(rightTest) forControlEvents:UIControlEventTouchDown];
}

#pragma 底部按钮事务处理

-(void)leftTest{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}
-(void)rightTest{
    NSString * name = self.textF.text;
    NSString * textStr = self.textView.text;
    [self.dict setValue:name forKey:@"name"];
    [self.dict setValue:textStr forKey:@"text"];
    if ([self.delegate respondsToSelector:@selector(alertCallBackDataWithDict:)]) {
        [self.delegate alertCallBackDataWithDict:self.dict];
    }
    [self leftTest];

}

#pragma 添加tableview到TextF
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"%lu",(unsigned long)self.alertArray.count);
    if (self.alertArray.count != 0) {
        [self addTableView];
    }
}

-(void)addTableView{
    UITableView * tableView = [[UITableView alloc] init];
    CGFloat x = 10;
    CGFloat y = CGRectGetMaxY(self.textF.frame) + 10;
    CGFloat W = self.textView.bounds.size.width;
    CGFloat H = 160;
    self.tableView = tableView;
    tableView.frame = CGRectMake(x, y, W, H);
    [self.alertView addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;

}
#pragma UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.alertArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:IDAlert];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDAlert];
    }
    cell.textLabel.text = self.alertArray[indexPath.row];


    return cell;
}
#pragma UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    self.textF.text = cell.textLabel.text;
    [self.tableView removeFromSuperview];
}
#pragma 点击屏幕取消编辑状态
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.tableView removeFromSuperview];
    [self.view endEditing:YES];
}
@end
互相学习,互相指正.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值