天天看点

iOS UIViewController+Alert

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (Alert)

- (void)showAlertController:(NSString *)title message:(NSString *)message;
- (void)showAlertController:(NSString *)title message:(NSString *)message complation:(void(^)(void))block;
- (void)showAlertControllerWithCancel:(NSString *)title message:(NSString *)message complation:(void(^)(void))block;

- (void)showActionSheetController:(NSString *)actionTitle complation:(void(^)(void))block;
- (void)showActionSheetController:(NSString *)title message:(NSString *)message actionTitle:(NSString *)actionTitle complation:(void(^)(void))block;

@end

NS_ASSUME_NONNULL_END
           
#import "UIViewController+Alert.h"

@implementation UIViewController (Alert)

- (void)showAlertController:(NSString *)title message:(NSString *)message {
    [self showAlertController:title message:message complation:^{
        
    }];
}

- (void)showAlertController:(NSString *)title message:(NSString *)message complation:(void(^)(void))block {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showAlertControllerWithCancel:(NSString *)title message:(NSString *)message complation:(void(^)(void))block {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark -
- (void)showActionSheetController:(NSString *)actionTitle complation:(void (^)(void))block {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showActionSheetController:(NSString *)title message:(NSString *)message actionTitle:(NSString *)actionTitle complation:(void(^)(void))block {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

@end