UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello" message:@"Habr!" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // }]];
typedef NS_ENUM(NSInteger, UIAlertDialogStyle) { UIAlertDialogStyleAlert = 0, UIAlertDialogStyleActionSheet };
typedef void(^UIAlertDialogHandler)(NSInteger buttonIndex);
@interface UIAlertDialog : NSObject <UIAlertViewDelegate, UIActionSheetDelegate> - (instancetype)initWithStyle:(UIAlertDialogStyle)style title:(NSString *)title andMessage:(NSString *)message; - (void)addButtonWithTitle:(NSString *)title andHandler:(UIAlertDialogHandler)handler; - (void)showInViewController:(UIViewController *)viewContoller; @end
- (instancetype)initWithStyle:(UIAlertDialogStyle)style title:(NSString *)title andMessage:(NSString *)message { if (self = [super init]) { self.style = style; self.title = title; self.message = message; self.items = [NSMutableArray new]; } return self; }
@interface UIAlertDialog () @property (nonatomic) UIAlertDialogStyle style; @property (copy, nonatomic) NSString *title; @property (copy, nonatomic) NSString *message; @property (strong, nonatomic) NSMutableArray *items; @end
- (void)addButtonWithTitle:(NSString *)title andHandler:(UIAlertDialogHandler)handler { UIAlertDialogItem *item = [UIAlertDialogItem new]; item.title = title; item.handler = handler; [self.items addObject:item]; }
@interface UIAlertDialogItem : NSObject @property (copy, nonatomic) NSString *title; @property (copy, nonatomic) UIAlertDialogHandler handler; @end
- (void)showInViewController:(UIViewController *)viewContoller { if ([[UIDevice currentDevice].systemVersion intValue] > 7) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self showAlertControllerInViewController:viewContoller]; }]; return; } if (self.style == UIAlertDialogStyleActionSheet) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self showActionSheetInView:viewContoller.view]; }]; } else { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self showAlert]; }]; } }
- (void)showAlertControllerInViewController:(UIViewController *)viewController { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:self.style == UIAlertDialogStyleActionSheet ? UIAlertControllerStyleActionSheet : UIAlertControllerStyleAlert]; NSInteger i = 0; for (UIAlertDialogItem *item in self.items) { UIAlertAction *alertAction = [UIAlertAction actionWithTitle:item.title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSInteger buttonIndex = i; if (item.handler) { item.handler(buttonIndex); } }]; [alertController addAction:alertAction]; i++; } UIAlertAction *closeAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"close", nil) style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:closeAction]; [viewController presentViewController:alertController animated:YES completion:nil]; }
NSInteger buttonIndex = i;
- (void)showMessage:(NSString *)message { UIAlertDialog *alertDialog = [[UIAlertDialog alloc] initWithStyle:UIAlertDialogStyleAlert title:message andMessage:nil]; [alertDialog showInViewController:self]; }
@interface UIAlertDialog : NSObject <UIAlertViewDelegate, UIActionSheetDelegate>
@interface UIAlertViewDialog : UIAlertView @property (strong, nonatomic) UIAlertDialog *alertDialog; @end
@interface UIActionSheetDialog : UIActionSheet @property (strong, nonatomic) UIAlertDialog *alertDialog; @end
- (void)showActionSheetInView:(UIView *)view { UIActionSheetDialog *actionSheet = [[UIActionSheetDialog alloc] initWithTitle:self.title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; actionSheet.alertDialog = self; for (UIAlertDialogItem *item in self.items) { [actionSheet addButtonWithTitle:item.title]; } [actionSheet addButtonWithTitle:NSLocalizedString(@"close", nil)]; actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1; [actionSheet showInView:view.window]; }
- (void)showAlert { UIAlertViewDialog *alertView = [[UIAlertViewDialog alloc] initWithTitle:self.title message:self.message delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; alertView.alertDialog = self; for (UIAlertDialogItem *item in self.items) { [alertView addButtonWithTitle:item.title]; } [alertView addButtonWithTitle:NSLocalizedString(@"close", nil)]; alertView.cancelButtonIndex = alertView.numberOfButtons - 1; [alertView show]; }
- (void)actionSheet:(UIActionSheetDialog *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == actionSheet.numberOfButtons - 1) { return; } UIAlertDialogItem *item = self.items[buttonIndex]; if (item.handler) { item.handler(buttonIndex); } }
- (void)alertView:(UIAlertViewDialog *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == alertView.numberOfButtons - 1) { return; } UIAlertDialogItem *item = self.items[buttonIndex]; if (item.handler) { item.handler(buttonIndex); } }
Source: https://habr.com/ru/post/242801/
All Articles