diff --git a/LGAlertView/LGAlertView.h b/LGAlertView/LGAlertView.h index a2e6ef5..f1885a0 100644 --- a/LGAlertView/LGAlertView.h +++ b/LGAlertView/LGAlertView.h @@ -35,6 +35,9 @@ static NSString *const kLGAlertViewWillShowNotification = @"LGAlertViewWillSh static NSString *const kLGAlertViewWillDismissNotification = @"LGAlertViewWillDismissNotification"; static NSString *const kLGAlertViewDidShowNotification = @"LGAlertViewDidShowNotification"; static NSString *const kLGAlertViewDidDismissNotification = @"LGAlertViewDidDismissNotification"; +static NSString *const kLGAlertViewActionNotification = @"LGAlertViewActionNotification"; +static NSString *const kLGAlertViewCancelNotification = @"LGAlertViewCancelNotification"; +static NSString *const kLGAlertViewDestructiveNotification = @"LGAlertViewDestructiveNotification"; @protocol LGAlertViewDelegate diff --git a/LGAlertView/LGAlertView.m b/LGAlertView/LGAlertView.m index 429954b..17a63e1 100644 --- a/LGAlertView/LGAlertView.m +++ b/LGAlertView/LGAlertView.m @@ -966,8 +966,6 @@ - (void)dealloc #if DEBUG NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__); #endif - - [self removeObservers]; } #pragma mark - Observers @@ -1076,6 +1074,13 @@ - (BOOL)textFieldShouldReturn:(LGAlertViewTextField *)textField return YES; } +#pragma mark - UIGestureRecognizer Delegate + +- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch +{ + return self.isCancelOnTouch; +} + #pragma mark - Setters and Getters - (void)setColorful:(BOOL)colorful @@ -1313,6 +1318,13 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath NSString *title = _buttonTitles[indexPath.row]; + // ----- + + [[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewActionNotification + object:self + userInfo:@{@"title" : title, + @"index" : [NSNumber numberWithInteger:index]}]; + if (_actionHandler) _actionHandler(self, title, index); if (_delegate && [_delegate respondsToSelector:@selector(alertView:buttonPressedWithTitle:index:)]) @@ -1473,9 +1485,6 @@ - (void)dismissComplete [_windowPrevious makeKeyAndVisible]; - self.viewController = nil; - self.window = nil; - // ----- [[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewDidDismissNotification object:self userInfo:nil]; @@ -1484,6 +1493,14 @@ - (void)dismissComplete if (_delegate && [_delegate respondsToSelector:@selector(alertViewDidDismiss:)]) [_delegate alertViewDidDismiss:self]; + + // ----- + + _view = nil; + _viewController = nil; + _windowNotice = nil; + _windowPrevious = nil; + _window = nil; } #pragma mark - @@ -2020,20 +2037,19 @@ - (void)layoutInvalidateWithSize:(CGSize)size - (void)cancelAction:(id)sender { - BOOL onButton = [sender isKindOfClass:[UIButton class]]; + BOOL onButton = ![sender isKindOfClass:[UIGestureRecognizer class]]; - if (sender) - { - if (onButton) - [(UIButton *)sender setSelected:YES]; - else if ([sender isKindOfClass:[UIGestureRecognizer class]] && !self.isCancelOnTouch) - return; - } + if (onButton) + [(UIButton *)sender setSelected:YES]; [self dismissAnimated:YES completionHandler:nil]; // ----- + [[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewCancelNotification + object:self + userInfo:@{@"onButton" : [NSNumber numberWithBool:onButton]}]; + if (_cancelHandler) _cancelHandler(self, onButton); if (_delegate && [_delegate respondsToSelector:@selector(alertViewCancelled:)]) @@ -2047,6 +2063,10 @@ - (void)destructiveAction:(id)sender [self dismissAnimated:YES completionHandler:nil]; + // ----- + + [[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewDestructiveNotification object:self userInfo:nil]; + if (_destructiveHandler) _destructiveHandler(self); if (_delegate && [_delegate respondsToSelector:@selector(alertViewDestructiveButtonPressed:)]) @@ -2063,6 +2083,13 @@ - (void)firstButtonAction NSString *title = _buttonTitles[0]; + // ----- + + [[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewActionNotification + object:self + userInfo:@{@"title" : title, + @"index" : [NSNumber numberWithInteger:index]}]; + if (_actionHandler) _actionHandler(self, title, index); if (_delegate && [_delegate respondsToSelector:@selector(alertView:buttonPressedWithTitle:index:)]) @@ -2079,6 +2106,13 @@ - (void)secondButtonAction NSString *title = _buttonTitles[1]; + // ----- + + [[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewActionNotification + object:self + userInfo:@{@"title" : title, + @"index" : [NSNumber numberWithInteger:index]}]; + if (_actionHandler) _actionHandler(self, title, index); if (_delegate && [_delegate respondsToSelector:@selector(alertView:buttonPressedWithTitle:index:)]) @@ -2128,13 +2162,9 @@ + (UIImage *)image1x1WithColor:(UIColor *)color + (void)keyboardAnimateWithNotificationUserInfo:(NSDictionary *)notificationUserInfo animations:(void(^)(CGFloat keyboardHeight))animations { - CGFloat keyboardHeight = (notificationUserInfo[@"UIKeyboardBoundsUserInfoKey"] ? [notificationUserInfo[@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height : 0.f); - if (!keyboardHeight) - keyboardHeight = (notificationUserInfo[UIKeyboardFrameBeginUserInfoKey] ? [notificationUserInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height : 0.f); - if (!keyboardHeight) - keyboardHeight = (notificationUserInfo[UIKeyboardFrameEndUserInfoKey] ? [notificationUserInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height : 0.f); - if (!keyboardHeight) - return; + CGFloat keyboardHeight = (notificationUserInfo[UIKeyboardFrameEndUserInfoKey] ? [notificationUserInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height : 0.f); + + if (!keyboardHeight) return; NSTimeInterval animationDuration = [notificationUserInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; int animationCurve = [notificationUserInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];