Skip to content

Commit 8028fb1

Browse files
committed
add notification support
1 parent 88ba364 commit 8028fb1

3 files changed

Lines changed: 56 additions & 5 deletions

File tree

SIAlertView/SIAlertView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#import <Foundation/Foundation.h>
1010

11+
extern NSString *const SIAlertViewWillShowNotification;
12+
extern NSString *const SIAlertViewDidShowNotification;
13+
extern NSString *const SIAlertViewWillDismissNotification;
14+
extern NSString *const SIAlertViewDidDismissNotification;
15+
1116
typedef NS_ENUM(NSInteger, SIAlertViewButtonType) {
1217
SIAlertViewButtonTypeDefault = 0,
1318
SIAlertViewButtonTypeDestructive,

SIAlertView/SIAlertView.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#import "SIAlertView.h"
1010
#import <QuartzCore/QuartzCore.h>
1111

12+
NSString *const SIAlertViewWillShowNotification = @"SIAlertViewWillShowNotification";
13+
NSString *const SIAlertViewDidShowNotification = @"SIAlertViewDidShowNotification";
14+
NSString *const SIAlertViewWillDismissNotification = @"SIAlertViewWillDismissNotification";
15+
NSString *const SIAlertViewDidDismissNotification = @"SIAlertViewDidDismissNotification";
16+
1217
#define DEBUG_LAYOUT 0
1318

1419
#define MESSAGE_MIN_LINE_COUNT 3
@@ -300,6 +305,7 @@ - (void)show
300305
if (self.willShowHandler) {
301306
self.willShowHandler(self);
302307
}
308+
[[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewWillShowNotification object:self userInfo:nil];
303309

304310
self.visible = YES;
305311

@@ -328,6 +334,7 @@ - (void)show
328334
if (self.didShowHandler) {
329335
self.didShowHandler(self);
330336
}
337+
[[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewDidShowNotification object:self userInfo:nil];
331338

332339
[SIAlertView setAnimating:NO];
333340

@@ -347,8 +354,11 @@ - (void)dismissAnimated:(BOOL)animated cleanup:(BOOL)cleanup
347354
{
348355
BOOL isVisible = self.isVisible;
349356

350-
if (isVisible && self.willDismissHandler) {
351-
self.willDismissHandler(self);
357+
if (isVisible) {
358+
if (self.willDismissHandler) {
359+
self.willDismissHandler(self);
360+
}
361+
[[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewWillDismissNotification object:self userInfo:nil];
352362
}
353363

354364
void (^dismissComplete)(void) = ^{
@@ -370,8 +380,11 @@ - (void)dismissAnimated:(BOOL)animated cleanup:(BOOL)cleanup
370380

371381
[SIAlertView setAnimating:NO];
372382

373-
if (isVisible && self.didDismissHandler) {
374-
self.didDismissHandler(self);
383+
if (isVisible) {
384+
if (self.didDismissHandler) {
385+
self.didDismissHandler(self);
386+
}
387+
[[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewDidDismissNotification object:self userInfo:nil];
375388
}
376389

377390
// check if we should show next alert
@@ -867,7 +880,6 @@ - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
867880
}
868881
}
869882

870-
871883
#pragma mark - UIAppearance setters
872884

873885
- (void)setTitleFont:(UIFont *)titleFont

SIAlertViewExample/SIAlertViewExample/ViewController.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ - (IBAction)alert2:(id)sender
126126
[alertView show];
127127
}
128128

129+
id observer1,observer2,observer3,observer4;
130+
129131
- (IBAction)alert3:(id)sender
130132
{
131133
SIAlertView *alertView = [[SIAlertView alloc] initWithTitle:nil andMessage:@"Message3"];
@@ -155,6 +157,38 @@ - (IBAction)alert3:(id)sender
155157
NSLog(@"%@, didDismissHandler3", alertView);
156158
};
157159

160+
observer1 = [[NSNotificationCenter defaultCenter] addObserverForName:SIAlertViewWillShowNotification
161+
object:alertView
162+
queue:[NSOperationQueue mainQueue]
163+
usingBlock:^(NSNotification *note) {
164+
NSLog(@"%@, -willShowHandler3", alertView);
165+
}];
166+
observer2 =[[NSNotificationCenter defaultCenter] addObserverForName:SIAlertViewDidShowNotification
167+
object:alertView
168+
queue:[NSOperationQueue mainQueue]
169+
usingBlock:^(NSNotification *note) {
170+
NSLog(@"%@, -didShowHandler3", alertView);
171+
}];
172+
observer3 =[[NSNotificationCenter defaultCenter] addObserverForName:SIAlertViewWillDismissNotification
173+
object:alertView
174+
queue:[NSOperationQueue mainQueue]
175+
usingBlock:^(NSNotification *note) {
176+
NSLog(@"%@, -willDismissHandler3", alertView);
177+
}];
178+
observer4 =[[NSNotificationCenter defaultCenter] addObserverForName:SIAlertViewDidDismissNotification
179+
object:alertView
180+
queue:[NSOperationQueue mainQueue]
181+
usingBlock:^(NSNotification *note) {
182+
NSLog(@"%@, -didDismissHandler3", alertView);
183+
184+
[[NSNotificationCenter defaultCenter] removeObserver:observer1];
185+
[[NSNotificationCenter defaultCenter] removeObserver:observer2];
186+
[[NSNotificationCenter defaultCenter] removeObserver:observer3];
187+
[[NSNotificationCenter defaultCenter] removeObserver:observer4];
188+
189+
observer1 = observer2 = observer3 = observer4 = nil;
190+
}];
191+
158192
[alertView show];
159193
}
160194

0 commit comments

Comments
 (0)