Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion PSTAlertController/PSTAlertController.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ typedef NS_ENUM(NSInteger, PSTAlertActionStyle) {
// Add action.
- (void)addAction:(PSTAlertAction *)action;

// Add block that is called before the alert controller will be presented (before animation).
- (void)addWillPresentBlock:(void (^)(void))willPresentBlock;

// Add block that is called before the alert controller has been presented (after animation).
- (void)addDidPresentBlock:(void (^)(void))didPresentBlock;

// Add block that is called after the alert controller will be dismissed (before animation).
- (void)addWillDismissBlock:(void (^)(PSTAlertAction *action))willDismissBlock;

// Add block that is called after the alert view has been dismissed (after animation).
// Add block that is called after the alert controller has been dismissed (after animation).
- (void)addDidDismissBlock:(void (^)(PSTAlertAction *action))didDismissBlock;

@property (nullable, nonatomic, copy, readonly) NSArray<PSTAlertAction *> *actions;
Expand Down
83 changes: 76 additions & 7 deletions PSTAlertController/PSTAlertController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ - (void)performAction {
@end

@interface PSTExtendedAlertController : UIAlertController
@property (nonatomic, copy) void (^viewWillAppearBlock)(void);
@property (nonatomic, copy) void (^viewDidAppearBlock)(void);
@property (nonatomic, copy) void (^viewWillDisappearBlock)(void);
@property (nonatomic, copy) void (^viewDidDisappearBlock)(void);
@end

@implementation PSTExtendedAlertController

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.viewWillAppearBlock) self.viewWillAppearBlock();
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.viewDidAppearBlock) self.viewDidAppearBlock();
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.viewWillDisappearBlock) self.viewWillDisappearBlock();
Expand All @@ -75,6 +87,8 @@ @interface PSTAlertController () <UIActionSheetDelegate, UIAlertViewDelegate> {
}
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(PSTAlertControllerStyle)preferredStyle NS_DESIGNATED_INITIALIZER;

@property (nonatomic, copy) NSArray *willPresentBlocks;
@property (nonatomic, copy) NSArray *didPresentBlocks;
@property (nonatomic, copy) NSArray *willDismissBlocks;
@property (nonatomic, copy) NSArray *didDismissBlocks;

Expand Down Expand Up @@ -345,18 +359,26 @@ - (void)showWithSender:(id)sender arrowDirection:(UIPopoverArrowDirection)arrowD

// Hook up dismiss blocks.
__weak typeof (self) weakSelf = self;
alertController.viewWillAppearBlock = ^{
typeof (self) strongSelf = weakSelf;
[strongSelf performPresentBlocks:PROPERTY(willPresentBlocks)];
};
alertController.viewDidAppearBlock = ^{
typeof (self) strongSelf = weakSelf;
[strongSelf performPresentBlocks:PROPERTY(didPresentBlocks)];
};
alertController.viewWillDisappearBlock = ^{
typeof (self) strongSelf = weakSelf;
[strongSelf performBlocks:PROPERTY(willDismissBlocks) withAction:strongSelf.executedAlertAction];
[strongSelf performDismissBlocks:PROPERTY(willDismissBlocks) withAction:strongSelf.executedAlertAction];
[strongSelf setIsShowingAlert:NO];
};
alertController.viewDidDisappearBlock = ^{
typeof (self) strongSelf = weakSelf;
[strongSelf performBlocks:PROPERTY(didDismissBlocks) withAction:strongSelf.executedAlertAction];
[strongSelf performDismissBlocks:PROPERTY(didDismissBlocks) withAction:strongSelf.executedAlertAction];
};

[controller presentViewController:alertController animated:animated completion:^{
// Bild lifetime of self to the controller.
// Bind lifetime of self to the controller.
// Will not be called if presenting fails because another present/dismissal already happened during that runloop.
// rdar://problem/19045528
objc_setAssociatedObject(controller, _cmd, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Expand Down Expand Up @@ -442,6 +464,16 @@ - (id)presentedObject {
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Will/Did Dismiss Observers

- (void)addWillPresentBlock:(void (^)(void))willPresentBlock {
NSParameterAssert(willPresentBlock);
self.willPresentBlocks = [[NSArray arrayWithArray:self.willPresentBlocks] arrayByAddingObject:willPresentBlock];
}

- (void)addDidPresentBlock:(void (^)(void))didPresentBlock {
NSParameterAssert(didPresentBlock);
self.didPresentBlocks = [[NSArray arrayWithArray:self.didPresentBlocks] arrayByAddingObject:didPresentBlock];
}

- (void)addWillDismissBlock:(void (^)(PSTAlertAction *action))willDismissBlock {
NSParameterAssert(willDismissBlock);
self.willDismissBlocks = [[NSArray arrayWithArray:self.willDismissBlocks] arrayByAddingObject:willDismissBlock];
Expand Down Expand Up @@ -470,7 +502,17 @@ - (PSTAlertAction *)actionForButtonIndex:(NSInteger)index {
return index >= 0 ? self.actions[index] : nil;
}

- (void)performBlocks:(NSString *)blocksStorageName withAction:(PSTAlertAction *)alertAction {
- (void)performPresentBlocks:(NSString *)blocksStorageName {
// Load variable and nil out.
NSArray *blocks = [self valueForKey:blocksStorageName];
[self setValue:nil forKey:blocksStorageName];

for (void (^block)(void) in blocks) {
block();
}
}

- (void)performDismissBlocks:(NSString *)blocksStorageName withAction:(PSTAlertAction *)alertAction {
// Load variable and nil out.
NSArray *blocks = [self valueForKey:blocksStorageName];
[self setValue:nil forKey:blocksStorageName];
Expand All @@ -480,12 +522,19 @@ - (void)performBlocks:(NSString *)blocksStorageName withAction:(PSTAlertAction *
}
}

- (void)viewWillPresent {
[self performPresentBlocks:PROPERTY(willPresentBlocks)];
}

- (void)viewDidPresent {
[self performPresentBlocks:PROPERTY(didPresentBlocks)];
}

- (void)viewWillDismissWithButtonIndex:(NSInteger)buttonIndex {
PSTAlertAction *action = [self actionForButtonIndex:buttonIndex];
self.executedAlertAction = action;

[self performBlocks:PROPERTY(willDismissBlocks) withAction:action];
self.willDismissBlocks = nil;
[self performDismissBlocks:PROPERTY(willDismissBlocks) withAction:action];

[self setIsShowingAlert:NO];
}
Expand All @@ -494,12 +543,22 @@ - (void)viewDidDismissWithButtonIndex:(NSInteger)buttonIndex {
PSTAlertAction *action = [self actionForButtonIndex:buttonIndex];
[action performAction];

[self performBlocks:PROPERTY(didDismissBlocks) withAction:action];
[self performDismissBlocks:PROPERTY(didDismissBlocks) withAction:action];
}

///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - UIActionSheetDelegate

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
[self viewWillPresent];
}

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
[self viewDidPresent];
}

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
[self viewWillDismissWithButtonIndex:buttonIndex];
}
Expand All @@ -512,6 +571,16 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - UIAlertViewDelegate

- (void)willPresentAlertView:(UIAlertView *)alertView
{
[self viewWillPresent];
}

- (void)didPresentAlertView:(UIAlertView *)alertView
{
[self viewDidPresent];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
[self viewWillDismissWithButtonIndex:buttonIndex];
}
Expand Down