Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Cocoanetics/DTFoundation
Conflicts: Core/Source/UIWebView+DTFoundation.h Core/Source/UIWebView+DTFoundation.m
- Loading branch information
Showing
14 changed files
with
577 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// DTActionSheet.h | ||
// DTFoundation | ||
// | ||
// Created by Oliver Drobnik on 08.06.12. | ||
// Copyright (c) 2012 Cocoanetics. All rights reserved. | ||
// | ||
|
||
typedef void (^DTActionSheetBlock)(void); | ||
|
||
/** | ||
Extends UIActionSheet with support for blocks | ||
*/ | ||
|
||
@interface DTActionSheet : UIActionSheet | ||
|
||
/** | ||
Initializes the action sheet using the specified title. | ||
*/ | ||
- (id)initWithTitle:(NSString *)title; | ||
|
||
/** | ||
Adds a custom button to the action sheet. | ||
@param title The title of the new button. | ||
@param block The block to execute when the button is tapped. | ||
@returns The index of the new button. Button indices start at 0 and increase in the order they are added. | ||
*/ | ||
- (NSInteger)addButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block; | ||
|
||
/** | ||
Adds a custom destructive button to the action sheet. | ||
Since there can only be one destructive button a previously marked destructive button becomes a normal button. | ||
@param title The title of the new button. | ||
@param block The block to execute when the button is tapped. | ||
@returns The index of the new button. Button indices start at 0 and increase in the order they are added. | ||
*/ | ||
- (NSInteger)addDestructiveButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block; | ||
|
||
/** | ||
Adds a custom cancel button to the action sheet. | ||
Since there can only be one cancel button a previously marked cancel button becomes a normal button. | ||
@param title The title of the new button. | ||
@returns The index of the new button. Button indices start at 0 and increase in the order they are added. | ||
*/ | ||
- (NSInteger)addCancelButtonWithTitle:(NSString *)title; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// | ||
// DTActionSheet.m | ||
// DTFoundation | ||
// | ||
// Created by Oliver Drobnik on 08.06.12. | ||
// Copyright (c) 2012 Cocoanetics. All rights reserved. | ||
// | ||
|
||
#import "DTActionSheet.h" | ||
|
||
@interface DTActionSheet () <UIActionSheetDelegate> | ||
|
||
@end | ||
|
||
@implementation DTActionSheet | ||
{ | ||
id <UIActionSheetDelegate> _externalDelegate; | ||
|
||
NSMutableDictionary *_actionsPerIndex; | ||
|
||
// lookup bitmask what delegate methods are implemented | ||
struct | ||
{ | ||
unsigned int delegateSupportsActionSheetCancel:1; | ||
unsigned int delegateSupportsWillPresentActionSheet:1; | ||
unsigned int delegateSupportsDidPresentActionSheet:1; | ||
unsigned int delegateSupportsWillDismissWithButtonIndex:1; | ||
unsigned int delegateSupportsDidDismissWithButtonIndex:1; | ||
} _delegateFlags; | ||
} | ||
|
||
- (id)initWithTitle:(NSString *)title | ||
{ | ||
self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; | ||
|
||
if (self) | ||
{ | ||
_actionsPerIndex = [[NSMutableDictionary alloc] init]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (NSInteger)addButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block | ||
{ | ||
NSInteger retIndex = [self addButtonWithTitle:title]; | ||
|
||
if (block) | ||
{ | ||
NSNumber *key = [NSNumber numberWithInt:retIndex]; | ||
[_actionsPerIndex setObject:[block copy] forKey:key]; | ||
} | ||
|
||
return retIndex; | ||
} | ||
|
||
- (NSInteger)addDestructiveButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block | ||
{ | ||
NSInteger retIndex = [self addButtonWithTitle:title block:block]; | ||
[self setDestructiveButtonIndex:retIndex]; | ||
|
||
return retIndex; | ||
} | ||
|
||
- (NSInteger)addCancelButtonWithTitle:(NSString *)title | ||
{ | ||
NSInteger retIndex = [self addButtonWithTitle:title]; | ||
[self setCancelButtonIndex:retIndex]; | ||
|
||
return retIndex; | ||
} | ||
|
||
#pragma UIActionSheetDelegate (forwarded) | ||
|
||
- (void)actionSheetCancel:(UIActionSheet *)actionSheet | ||
{ | ||
if (_delegateFlags.delegateSupportsActionSheetCancel) | ||
{ | ||
[_externalDelegate actionSheetCancel:actionSheet]; | ||
} | ||
} | ||
|
||
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet | ||
{ | ||
if (_delegateFlags.delegateSupportsWillPresentActionSheet) | ||
{ | ||
[_externalDelegate willPresentActionSheet:actionSheet]; | ||
} | ||
} | ||
|
||
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet | ||
{ | ||
if (_delegateFlags.delegateSupportsDidPresentActionSheet) | ||
{ | ||
[_externalDelegate didPresentActionSheet:actionSheet]; | ||
} | ||
} | ||
|
||
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex | ||
{ | ||
if (_delegateFlags.delegateSupportsWillDismissWithButtonIndex) | ||
{ | ||
[_externalDelegate actionSheet:actionSheet willDismissWithButtonIndex:buttonIndex]; | ||
} | ||
} | ||
|
||
|
||
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex | ||
{ | ||
NSNumber *key = [NSNumber numberWithInt:buttonIndex]; | ||
|
||
DTActionSheetBlock block = [_actionsPerIndex objectForKey:key]; | ||
|
||
if (block) | ||
{ | ||
block(); | ||
} | ||
|
||
if (_delegateFlags.delegateSupportsDidDismissWithButtonIndex) | ||
{ | ||
[_externalDelegate actionSheet:actionSheet didDismissWithButtonIndex:buttonIndex]; | ||
} | ||
} | ||
|
||
|
||
#pragma mark Properties | ||
|
||
- (id <UIActionSheetDelegate>)delegate | ||
{ | ||
return _externalDelegate; | ||
} | ||
|
||
- (void)setDelegate:(id <UIActionSheetDelegate>)delegate | ||
{ | ||
if (delegate == self) | ||
{ | ||
[super setDelegate:self]; | ||
} | ||
else if (delegate == nil) | ||
{ | ||
[super setDelegate:nil]; | ||
_externalDelegate = nil; | ||
} | ||
else | ||
{ | ||
_externalDelegate = delegate; | ||
} | ||
|
||
// wipe | ||
memset(&_delegateFlags, 0, sizeof(_delegateFlags)); | ||
|
||
// set flags according to available methods in delegate | ||
if ([_externalDelegate respondsToSelector:@selector(actionSheetCancel:)]) | ||
{ | ||
_delegateFlags.delegateSupportsActionSheetCancel = YES; | ||
} | ||
|
||
if ([_externalDelegate respondsToSelector:@selector(willPresentActionSheet:)]) | ||
{ | ||
_delegateFlags.delegateSupportsWillPresentActionSheet = YES; | ||
} | ||
|
||
if ([_externalDelegate respondsToSelector:@selector(didPresentActionSheet:)]) | ||
{ | ||
_delegateFlags.delegateSupportsDidPresentActionSheet = YES; | ||
} | ||
|
||
if ([_externalDelegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) | ||
{ | ||
_delegateFlags.delegateSupportsWillDismissWithButtonIndex = YES; | ||
} | ||
|
||
if ([_externalDelegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) | ||
{ | ||
_delegateFlags.delegateSupportsDidDismissWithButtonIndex = YES; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.