Skip to content

Commit

Permalink
Add willDismissWithButtonIndex: and didDismissWithButtonIndex: option…
Browse files Browse the repository at this point in the history
…al protocol methods
  • Loading branch information
Rob Hudson committed Apr 27, 2015
1 parent a4b52f1 commit de73fd6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions IBActionSheetSample/IBActionSheetSample/IBActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ typedef void (^IBActionCallback)(IBActionSheet *actionSheet, NSInteger buttonInd

-(void)actionSheet:(IBActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

@optional
-(void)actionSheet:(IBActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
-(void)actionSheet:(IBActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;

@end

#pragma mark - IBActionSheet
Expand Down
26 changes: 22 additions & 4 deletions IBActionSheetSample/IBActionSheetSample/IBActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#pragma mark - IBActionSheet

@implementation IBActionSheet
{
NSInteger _selectedButtonIndex;
}

#pragma mark IBActionSheet Set up methods

Expand Down Expand Up @@ -616,14 +619,23 @@ - (NSInteger)addButtonWithTitle:(NSString *)title {

- (void)buttonClicked:(IBActionSheetButton *)button {

if(self.delegate) [self.delegate actionSheet:self clickedButtonAtIndex:button.index];
_selectedButtonIndex = button.index;
if(self.delegate) [self.delegate actionSheet:self clickedButtonAtIndex:_selectedButtonIndex];
if(self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self willDismissWithButtonIndex:_selectedButtonIndex];
}
if(self.callback) self.callback(self, button.index);
self.shouldCancelOnTouch = YES;
[self removeFromView];
}

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {

_selectedButtonIndex = buttonIndex;
if(self.delegate) [self.delegate actionSheet:self clickedButtonAtIndex:_selectedButtonIndex];
if(self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self willDismissWithButtonIndex:_selectedButtonIndex];
}
if (!animated) {
[self.transparentView removeFromSuperview];
UIView *blurView = [self.superview viewWithTag:821];
Expand All @@ -632,18 +644,18 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim
}
[self removeFromSuperview];
self.visible = NO;
if(self.delegate) [self.delegate actionSheet:self clickedButtonAtIndex:buttonIndex];
if(self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self didDismissWithButtonIndex:_selectedButtonIndex];
}
if(self.callback) self.callback(self, buttonIndex);
} else {
[self removeFromView];
if(self.delegate) [self.delegate actionSheet:self clickedButtonAtIndex:buttonIndex];
if(self.callback) self.callback(self, buttonIndex);
}
}

- (void)showInView:(UIView *)theView {


UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

Expand Down Expand Up @@ -732,6 +744,9 @@ - (void)removeFromView {
[self.transparentView removeFromSuperview];
[self removeFromSuperview];
self.visible = NO;
if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self didDismissWithButtonIndex:_selectedButtonIndex];
}
}];
} else {

Expand All @@ -758,6 +773,9 @@ - (void)removeFromView {
[blurView removeFromSuperview];
}
self.visible = NO;
if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self didDismissWithButtonIndex:_selectedButtonIndex];
}
}];
}

Expand Down
7 changes: 7 additions & 0 deletions IBActionSheetSample/IBActionSheetSample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ - (void)actionSheet:(IBActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger
NSLog(@"Button at index: %ld clicked\nIts title is '%@'", (long)buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
}

// optional delegate methods
- (void)actionSheet:(IBActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"Will dismiss with button index %ld", (long)buttonIndex);
}

- (void)actionSheet:(IBActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"Dismissed with button index %ld", (long)buttonIndex);
}

#pragma mark - All the other junk for the sample project

Expand Down

0 comments on commit de73fd6

Please sign in to comment.