IOS 自定义UIAlertController

时间:2024-03-26 17:14:18

自定义UIAlertController:

首先展示效果图
IOS 自定义UIAlertControllerIOS 自定义UIAlertController
1.创建一个新的类来管理弹出的视图(继承于UIView)

IOS 自定义UIAlertController

2.传建一个xib文件来自定义弹出视图(注意创建过后一定要将xib的class关联)

IOS 自定义UIAlertController

3.在xib里面自定义自己想要弹出视图的样式(这里可以再拖一个View并关联同一个Class 在使用的时候选择一个使用)

IOS 自定义UIAlertController

4.关联按钮和点击事件(如果有两个View 关联按钮和事件的时候不要去创建两个,如图直接拖动)

IOS 自定义UIAlertController
IOS 自定义UIAlertController
上下和左右是我的两个VIew 的名字

IOS 自定义UIAlertController

IOS 自定义UIAlertController

5.最后就是提供外部调用类的创建方法和事件和按钮的触发

这是CustomAlertView.h的代码
#import <UIKit/UIKit.h>
/回调/
typedef void(^SureButtonBlock)(void);
/**/
typedef void(^CancelBlock)(void);

typedef enum{
kAlertViewStyleUpAndDown,
kAlertViewStyleLeftAndRight,
}kAlertViewStyle;

@interface CustomAlertView : UIView
/**
快速创建CustomAlertView的一个对象
*/
+(instancetype)alertViewWithImageName:(NSString )imageName title:(NSString )title style:(kAlertViewStyle)style;
/

在视图上显示
*/
-(void)show;
-(void)showInView:(UIView *)superView;

/** 删除提示视图
*/
-(void)dismiss;

-(void)okButtonTitle:(NSString *)title complished:(SureButtonBlock)block;
-(void)cancelButtonTitle:(NSString *)title complished:(CancelBlock)block;
@end`

这是CustomAlertView.m的代码

#import "CustomAlertView.h"
#import "AppDelegate.h"
@interface CustomAlertView ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;


//对于Block 使用copy
@property(nonatomic,copy) SureButtonBlock okBlock;
@property(nonatomic,copy) CancelBlock cancelBlock;
@end
@implementation CustomAlertView

//类方法里面不能直接访问属性变量和实例方法
+(instancetype)alertViewWithImageName:(NSString *)imageName title:(NSString *)title style:(kAlertViewStyle)style{
    //1.读取xib文件
    NSArray *nibsArray = [[NSBundle mainBundle]loadNibNamed:@"CustomAlertView" owner:nil options:nil];
    //2.根据用户的style读取对应的View
    CustomAlertView *alertView = [CustomAlertView new];
    if (style == kAlertViewStyleUpAndDown) {
    alertView = [nibsArray objectAtIndex:0];
    }else{
    alertView = [nibsArray objectAtIndex:1];
        
    }
    if (imageName != nil) {
        alertView.iconImageView.image = [UIImage imageNamed:imageName];
    };
    if (title != nil) {
        alertView.titleLabel.text = title;
    };
    return alertView;
}

-(void)show{
    //找到当前界面的视图
    //将这个视图添加到当前界面的最外层
//    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [app.window  addSubview:self];
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [app.window.rootViewController.view addSubview:self];
    
}
-(void)showInView:(UIView *)superView{
    [superView addSubview:self];
}

-(void)dismiss{
    [self removeFromSuperview];
}

- (IBAction)okButtonDidClicked:(UIButton *)sender {
    [self dismiss];
    self.okBlock();
}
- (IBAction)cancelButtonDidClicked:(UIButton *)sender {
    self.cancelBlock();
    [self dismiss];
}

-(void)okButtonTitle:(NSString *)title complished:(SureButtonBlock)block{
    [_okButton setTitle:title forState:UIControlStateNormal];
    self.okBlock = block;
}
-(void)cancelButtonTitle:(NSString *)title complished:(CancelBlock)block{
    [_cancelButton setTitle:title forState:UIControlStateNormal];
    self.cancelBlock = block;
}

6.外部使用的例子

CustomAlertView *alertView = [CustomAlertView alertViewWithImageName:@"gou" title:@"yes" style:kAlertViewStyleLeftAndRight];
    
    [alertView okButtonTitle:@"发送" complished:^{
        NSLog(@"111");
    }];
    [alertView cancelButtonTitle:@"" complished:^{
    }];
    [alertView show];