Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlimited buttons #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions UIActionSheet+Blocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,36 @@
static const void *UIActionSheetDidDismissBlockKey = &UIActionSheetDidDismissBlockKey;
static const void *UIActionSheetCancelBlockKey = &UIActionSheetCancelBlockKey;

#define NSArrayObjectMaybeNil(__ARRAY__, __INDEX__) ((__INDEX__ >= [__ARRAY__ count]) ? nil : [__ARRAY__ objectAtIndex:__INDEX__])
// This is a hack to turn an array into a variable argument list. There is no good way to expand arrays into variable argument lists in Objective-C. This works by nil-terminating the list as soon as we overstep the bounds of the array. The obvious glitch is that we only support a finite number of buttons.
#define NSArrayToVariableArgumentsList(__ARRAYNAME__) NSArrayObjectMaybeNil(__ARRAYNAME__, 0), NSArrayObjectMaybeNil(__ARRAYNAME__, 1), NSArrayObjectMaybeNil(__ARRAYNAME__, 2), NSArrayObjectMaybeNil(__ARRAYNAME__, 3), NSArrayObjectMaybeNil(__ARRAYNAME__, 4), NSArrayObjectMaybeNil(__ARRAYNAME__, 5), NSArrayObjectMaybeNil(__ARRAYNAME__, 6), NSArrayObjectMaybeNil(__ARRAYNAME__, 7), NSArrayObjectMaybeNil(__ARRAYNAME__, 8), NSArrayObjectMaybeNil(__ARRAYNAME__, 9), nil

@implementation UIActionSheet (Blocks)


- (instancetype)initWithTitle:(NSString *)title
delegate:(id<UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitlesArray:(NSArray *)otherButtonTitles {

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
if (destructiveButtonTitle) {
actionSheet.destructiveButtonIndex = [actionSheet addButtonWithTitle:destructiveButtonTitle];
}

for (NSString *title in otherButtonTitles) {
[actionSheet addButtonWithTitle:title];
}

if (cancelButtonTitle) {
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:cancelButtonTitle];
}

return actionSheet;
}

+ (instancetype)showFromTabBar:(UITabBar *)tabBar
withTitle:(NSString *)title
cancelButtonTitle:(NSString *)cancelButtonTitle
Expand All @@ -56,7 +80,7 @@ + (instancetype)showFromTabBar:(UITabBar *)tabBar
delegate:nil
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:NSArrayToVariableArgumentsList(otherButtonTitles)];
otherButtonTitlesArray:otherButtonTitles];

if (tapBlock) {
actionSheet.tapBlock = tapBlock;
Expand All @@ -82,7 +106,7 @@ + (instancetype)showFromToolbar:(UIToolbar *)toolbar
delegate:nil
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:NSArrayToVariableArgumentsList(otherButtonTitles)];
otherButtonTitlesArray:otherButtonTitles];

if (tapBlock) {
actionSheet.tapBlock = tapBlock;
Expand All @@ -108,7 +132,7 @@ + (instancetype)showInView:(UIView *)view
delegate:nil
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:NSArrayToVariableArgumentsList(otherButtonTitles)];
otherButtonTitlesArray:otherButtonTitles];

if (tapBlock) {
actionSheet.tapBlock = tapBlock;
Expand All @@ -135,7 +159,7 @@ + (instancetype)showFromBarButtonItem:(UIBarButtonItem *)barButtonItem
delegate:nil
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:NSArrayToVariableArgumentsList(otherButtonTitles)];
otherButtonTitlesArray:otherButtonTitles];

if (tapBlock) {
actionSheet.tapBlock = tapBlock;
Expand Down Expand Up @@ -163,7 +187,7 @@ + (instancetype)showFromRect:(CGRect)rect
delegate:nil
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:NSArrayToVariableArgumentsList(otherButtonTitles)];
otherButtonTitlesArray:otherButtonTitles];

if (tapBlock) {
actionSheet.tapBlock = tapBlock;
Expand Down
4 changes: 2 additions & 2 deletions UIActionSheet+Blocks.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "UIActionSheet+Blocks"
s.version = "0.8.1"
s.version = "0.8.2"
s.summary = "Category on UIActionSheet to use inline block callbacks instead of delegate callbacks."

s.description = <<-DESC
Expand All @@ -14,7 +14,7 @@ Pod::Spec.new do |s|
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Ryan Maxwell" => "[email protected]" }
s.platform = :ios, '4.3'
s.source = { :git => "https://github.com/ryanmaxwell/UIActionSheet-Blocks.git", :tag => "0.8.1" }
s.source = { :git => "https://github.com/ryanmaxwell/UIActionSheet-Blocks.git", :tag => "0.8.2" }
s.source_files = 'UIActionSheet+Blocks.{h,m}'
s.requires_arc = true
end