Skip to content

Commit

Permalink
Fixed some buds and add notifications for action, cancel and destruct…
Browse files Browse the repository at this point in the history
…ive handlers
  • Loading branch information
Admin authored and Admin committed Oct 28, 2015
1 parent 828f4ae commit 6010cfb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
3 changes: 3 additions & 0 deletions LGAlertView/LGAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NSObject>

Expand Down
70 changes: 50 additions & 20 deletions LGAlertView/LGAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,6 @@ - (void)dealloc
#if DEBUG
NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
#endif

[self removeObservers];
}

#pragma mark - Observers
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:)])
Expand Down Expand Up @@ -1473,9 +1485,6 @@ - (void)dismissComplete

[_windowPrevious makeKeyAndVisible];

self.viewController = nil;
self.window = nil;

// -----

[[NSNotificationCenter defaultCenter] postNotificationName:kLGAlertViewDidDismissNotification object:self userInfo:nil];
Expand All @@ -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 -
Expand Down Expand Up @@ -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:)])
Expand All @@ -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:)])
Expand All @@ -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:)])
Expand All @@ -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:)])
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 6010cfb

Please sign in to comment.