ios 三种系统弹框的封装(UIAlertController,UIActionSheet,输入框)

下载地址

在这里插入图片描述
一句代码搞定三种弹框(面向协议的编程)
第一步:代码的架构看图(定义一个OutgoingServer,)我们只需要调用这个,
在这里插入图片描述
第二步: 从OutgoingServer获得弹框

+ (id<ActionSheetAlertInterfaces>)actionSheetWithTitle:(NSString *)title
                                               message:(NSString *)message
                                        preferredStyle:(UIAlertControllerStyle)preferredStyle
                                        

第三步实现:实现弹框,定一个协议,

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^ActionSheetBlock)(void);
typedef void(^ActionSheetTextFieldBlock)(UITextField * _Nonnull textField);

NS_ASSUME_NONNULL_BEGIN

@protocol ActionSheetAlertInterfaces <NSObject>

@optional

- (void)addAction:(NSString *)title
            style:(UIAlertActionStyle)style
      finishBlock:(ActionSheetBlock)block;

- (void)addTextFieldWithFinishBlock:(ActionSheetTextFieldBlock)block;

@required

/**
 <#Description#>

 @param title titile
 @param message message
 @param preferredStyle (Sheet,Alert)
 @return <#return value description#>
 */
- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
               preferredStyle:(UIAlertControllerStyle)preferredStyle;

/**
 

 @param superVc 父视图
 */
- (void)showInSuperVc:(UIViewController *)superVc;

第四部实现协议:

#import "SheetViewController.h"

@interface SheetViewController ()

@property (strong, nonatomic) UIAlertController *actionSheet;

@end

@implementation SheetViewController

- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
               preferredStyle:(UIAlertControllerStyle)preferredStyle
{
    
    if (self = [super init])
    {
        self.actionSheet =  [UIAlertController alertControllerWithTitle:title
                                                                message:message
                                                    preferredStyle:preferredStyle];
        
    }
    
    return self;
}


- (void)addAction:(NSString *)title style:(UIAlertActionStyle)style finishBlock:(ActionSheetBlock)block
{
    
    
    __weak typeof(self)weakSelf = self;
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:title  style:style handler:^(UIAlertAction *action) {
        
        if (block)
        {
            block();
        }
        
        [weakSelf removeFromParentViewController];
    }];
    
    [self.actionSheet addAction:alertAction];
    
}

- (void)addTextFieldWithFinishBlock:(ActionSheetTextFieldBlock)block
{
    [self.actionSheet addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        if (block) {
            block(textField);
        }
        
    }];
}

- (void)showInSuperVc:(UIViewController *)superVc{
    
    [superVc presentViewController:self.actionSheet animated:YES completion:nil];
    
}


@end

第五步:调用

 id<ActionSheetAlertInterfaces> actionSheet = [ActionSheetAlertFactory actionSheetWithTitle:@"选择" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        
        [actionSheet addAction:@"取消" style:UIAlertActionStyleCancel finishBlock:^{
            
        }];
        
        [actionSheet addAction:@"确定" style:UIAlertActionStyleDestructive finishBlock:^{
            
        }];
        [actionSheet addTextFieldWithFinishBlock:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入评语";
            
        }];
       [actionSheet showInSuperVc:self];
      
发布了128 篇原创文章 · 获赞 106 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/Z1591090/article/details/88344066