@@ -32,6 +32,7 @@ - (void)viewController:(SIAlertViewController *)viewController clickedButtonInde
3232static NSMutableArray *__si_alert_queue;
3333static BOOL __si_alert_animating;
3434static SIBackgroundWindow *__si_alert_background_window;
35+ static SIAlertView *__si_alert_current_view;
3536
3637@interface SIAlertView () <SIAlertViewControllerDelegate>
3738
@@ -44,6 +45,7 @@ @interface SIAlertView () <SIAlertViewControllerDelegate>
4445
4546+ (SIBackgroundWindow *)sharedBackground ;
4647+ (NSMutableArray *)sharedQueue ;
48+ + (SIAlertView *)currentAlertView ;
4749+ (BOOL )isAnimating ;
4850+ (void )setAnimating : (BOOL )animating ;
4951+ (void )removeSharedBackground ;
@@ -609,8 +611,9 @@ - (void)buttonAction:(UIButton *)button
609611- (void )animationDidStop : (CAAnimation *)anim finished : (BOOL )flag
610612{
611613 if (self.transitionCompletion ) {
612- self.transitionCompletion ();
614+ void (^completion)( void ) = [ self .transitionCompletion copy ]; // copy block to prevent override
613615 self.transitionCompletion = nil ;
616+ completion ();
614617 }
615618}
616619
@@ -655,6 +658,16 @@ + (NSMutableArray *)sharedQueue
655658 return __si_alert_queue;
656659}
657660
661+ + (SIAlertView *)currentAlertView
662+ {
663+ return __si_alert_current_view;
664+ }
665+
666+ + (void )setCurrentAlertView : (SIAlertView *)alertView
667+ {
668+ __si_alert_current_view = alertView;
669+ }
670+
658671+ (BOOL )isAnimating
659672{
660673 return __si_alert_animating;
@@ -706,6 +719,16 @@ - (void)show
706719 return ; // wait for next turn
707720 }
708721
722+ if (self.isVisible ) {
723+ return ;
724+ }
725+
726+ if ([SIAlertView currentAlertView ].isVisible ) {
727+ SIAlertView *alert = [SIAlertView currentAlertView ];
728+ [alert dismissAnimated: YES cleanup: NO ];
729+ return ;
730+ }
731+
709732 if (self.willShowHandler ) {
710733 self.willShowHandler (self);
711734 }
@@ -732,6 +755,7 @@ - (void)show
732755 self.visible = YES ;
733756
734757 [SIAlertView setAnimating: YES ];
758+ [SIAlertView setCurrentAlertView: self ];
735759
736760 [self .viewController transitionInCompletion: ^{
737761 if (self.didShowHandler ) {
@@ -740,21 +764,40 @@ - (void)show
740764
741765 [SIAlertView setAnimating: NO ];
742766
743- [self showNextIfNeededForAlert: self ];
767+ NSInteger index = [[SIAlertView sharedQueue ] indexOfObject: self ];
768+ if (index < [SIAlertView sharedQueue ].count - 1 ) {
769+ [self dismissAnimated: YES cleanup: NO ]; // dismiss to show next alert view
770+ }
744771 }];
772+ }
773+
774+ + (void )cleanUpForAlertView : (SIAlertView *)alertView
775+ {
776+ alertView.visible = NO ;
777+
778+ [alertView.alertWindow removeFromSuperview ];
779+ alertView.alertWindow = nil ;
780+ alertView.viewController = nil ;
781+
782+ [[SIAlertView sharedQueue ] removeObject: alertView];
783+
784+ [SIAlertView setAnimating: NO ];
745785
746- // hide previous alert
747- NSInteger index = [[SIAlertView sharedQueue ] indexOfObject: self ];
748- if (index > 0 ) {
749- SIAlertView *alert = [SIAlertView sharedQueue ][index - 1 ];
750- alert.alertWindow .hidden = YES ;
751- alert.visible = NO ;
786+ if (alertView.didDismissHandler ) {
787+ alertView.didDismissHandler (alertView);
752788 }
753789}
754790
755- - (void )dismissWithClickedButtonIndex : ( NSInteger ) buttonIndex animated : (BOOL )animated
791+ - (void )dismissAnimated : (BOOL )animated
756792{
757- if (self.willDismissHandler ) {
793+ [self dismissAnimated: animated cleanup: YES ];
794+ }
795+
796+ - (void )dismissAnimated : (BOOL )animated cleanup : (BOOL )cleanup
797+ {
798+ BOOL isVisible = self.isVisible ;
799+
800+ if (isVisible && self.willDismissHandler ) {
758801 self.willDismissHandler (self);
759802 }
760803
@@ -765,37 +808,36 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim
765808 self.alertWindow = nil ;
766809 self.viewController = nil ;
767810
768- [[SIAlertView sharedQueue ] removeObject: self ];
811+ [SIAlertView setCurrentAlertView: nil ];
812+
813+ SIAlertView *nextAlertView;
814+ NSInteger index = [[SIAlertView sharedQueue ] indexOfObject: self ];
815+ if (index != NSNotFound && index < [SIAlertView sharedQueue ].count - 1 ) {
816+ nextAlertView = [SIAlertView sharedQueue ][index + 1 ];
817+ }
818+
819+ if (cleanup) {
820+ [[SIAlertView sharedQueue ] removeObject: self ];
821+ }
769822
770823 [SIAlertView setAnimating: NO ];
771824
772- if (self.didDismissHandler ) {
825+ if (isVisible && self.didDismissHandler ) {
773826 self.didDismissHandler (self);
774827 }
775828
776- if ([SIAlertView sharedQueue ].count > 0 ) {
777-
778- SIAlertView *alert = [[SIAlertView sharedQueue ] lastObject ];
779-
780- if (alert.viewController ) {
781- // show previous alert
782- alert.alertWindow .hidden = NO ;
783- alert.visible = YES ;
784-
785- // transition in again
786- [alert.viewController transitionInCompletion: ^{
787- [SIAlertView setAnimating: NO ];
788-
789- [self showNextIfNeededForAlert: alert];
790- }];
791- } else {
792- // show new added alert
829+ if (nextAlertView) {
830+ [nextAlertView show ];
831+ } else {
832+ // show last alert view
833+ if ([SIAlertView sharedQueue ].count > 0 ) {
834+ SIAlertView *alert = [[SIAlertView sharedQueue ] lastObject ];
793835 [alert show ];
794836 }
795837 }
796838 };
797839
798- if (animated) {
840+ if (animated && isVisible ) {
799841 [SIAlertView setAnimating: YES ];
800842 [self .viewController transitionOutCompletion: dismissComplete];
801843
@@ -806,33 +848,22 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim
806848 } else {
807849 dismissComplete ();
808850
809- if ([SIAlertView sharedQueue ].count == 1 ) {
851+ if ([SIAlertView sharedQueue ].count == 0 ) {
810852 [[SIAlertView sharedBackground ] hideAnimated: NO ];
811853 }
812854 }
813855}
814856
815- #pragma mark - Private
816-
817- - (void )showNextIfNeededForAlert : (SIAlertView *)alert
818- {
819- NSInteger index = [[SIAlertView sharedQueue ] indexOfObject: alert];
820- if (index < [SIAlertView sharedQueue ].count - 1 ) {
821- SIAlertView *alert = [SIAlertView sharedQueue ][index + 1 ];
822- [alert show ];
823- }
824- }
825-
826857#pragma mark - SIAlertViewControllerDelegate
827858
828859- (void )viewController : (SIAlertViewController *)viewController clickedButtonIndex : (NSInteger )buttonIndex
829860{
830- [SIAlertView setAnimating: YES ];
861+ [SIAlertView setAnimating: YES ]; // set this flag to YES in order to prevent showing another alert in action block
831862 SIAlertItem *item = self.items [buttonIndex];
832863 if (item.action ) {
833864 item.action (self);
834865 }
835- [self dismissWithClickedButtonIndex: buttonIndex animated :YES ];
866+ [self dismissAnimated :YES ];
836867}
837868
838869#pragma mark - UIAppearance getters
0 commit comments